Lesson 3: Programming Mathematica, 1 of 3

Some Terms

Notebook

Cell

Style

Input

Output

Front End

Pallete

Demonstration

Player

Kernel

Language Basics

?InputForm

?StandardForm

?TraditionalForm

Case Sensitivity :

a==A //Simplify

a==a //Simplify

Order of operations :

(2+4)*3

2+4 3

Functions :

f[x]

f(x)

Creating One Dimensional Lists

{2, 3, -5}

{x, 5, "hi"}

Table[(i^2),{i,5}]

Table[a, {4}]

Table[(i^2),{i,0,1,0.3}]

Range[5]

Table[i, {5}]

?Table

ConstantArray[23,4]

Manipulating Lists

list1={a,b,c,d,e}

Length[list1]

list1[[2]]

Take[list1,3]

?Take

Take[list1,{2,4}]

list1[[2;;4]]

?Part

RotateRight[list1]

NestList[RotateRight,list1,Length[list1]]

NestList[RotateRight,list1,Length[list1]] //MatrixForm

?RotateRight

Sort[RotateRight[list1,3]]

Join[list1,{f,g}]

Join[list1,x]

Flatten[NestList[RotateRight,list1,Length[list1]] ]

list2={(a^2),b,b,c,d^2,b,e}

Position[list2,b]

Extract[list2,%]

Position[list2,_^2]

Extract[list2,%]

myTest[x_]=(x>10)&&(x<50)

Select[Table[(i^2),{i,10}],myTest]

Multidimensional Lists

testmat = Table[(a^i)+(b^j),{i,3},{j,3}]

MatrixForm[testmat]

Position[testmat,_^2+_]

Extract[testmat,%]

Map[g,testmat]

?Map

Map[g,testmat,2]

% //MatrixForm

Map[g,testmat,{2}]

% //MatrixForm

Example of lists: Sampling from the uniform distribution

ourData=Table[Random[],{10000}];

Length[ourData]

Short[ourData]

?Mean

Mean[ourData]

Variance[ourData]

Does it seem like Random[] generated Uniform Random Variables?

?UniformDistribution

?*Distribution

Mean[UniformDistribution[{0,1}]]

Variance[UniformDistribution[{0,1}]]

%//N

f[x_]:=PDF[UniformDistribution[{0,1}],x]

f[x]

Plot[f[x],{x,-1,2},PlotStyle->Thickness[0.01]]

Integrate[f[x],{x,-Infinity,Infinity}]

Integrate[ x f[x],{x,-Infinity,Infinity}]

Integrate[ ((x )^2) f[x],{x,-Infinity,Infinity}]

%31-((%30)^2)

?Random*

?RandomSample

sample=RandomSample[ourData,30]

Mean[sample]

samples=Table[RandomSample[ourData,30],{100}];

samples[[1]]

samples[[100]]

means=Map[Mean,samples]

Variance[means]

exactVarOfUniformSample[n_]:=((Variance[UniformDistribution[{0,1}]])/n)

exactVarOfUniformSample[30] //N

samples=Table[RandomSample[ourData,30],{1000}];

means=Map[Mean,samples];

Length[means]

Variance[means]

Manipulate[g[n],
{n,10,40,10}]

ourData=Table[Random[],{10000}];
Manipulate[
samples=Table[RandomSample[ourData,n],{1000}];
means=Map[Mean,samples];
{(1/(12 n))//N, Variance[means]}
,
{n,10,40,10}]