(* Student Name *)
(* July 24, 2018 *)
(* Physics III - Introduction to Statistical Physics *)
(* Assignment 5 - Problem X*)

(a) Mean and Standard Deviation of Binomial Distribution

(* *)

(b) Function Definitions

?For

?RandomReal

?If

?Print

?Range

?AppendTo

?Histogram

?Mean

?StandardDeviation

?N

(c) Simple Code Examples

Simple For-Loop

For[i = 0, i <5, i++, Print[i]](* *)

Single Coin Toss Simulation

x=RandomReal[]; (* *)
If[x > 0.5,Print[H], Print[T]];(* *)

Three Coin Tosses Simulation

For[k=1,k<4,k++,x=RandomReal[]; (* *)
If[x > 0.5,Print[H], Print[T]]] (* *)

Element Extraction

vec = {2,4,6,8,10,12,14,16,18,20};  (* *)
vec[[4]](* *)

Element appending

vec = {1, 2, 3}; (* *)
Print[vec] (* *)
AppendTo[vec, 4]; (* *)
Print[vec] (* *)

(Simulating Coin Flips)

(d) Single Coin-Flip Simulations

Nflips = 10; (* *)
flip = Range[0,Nflips-1]; (* *)
HeadsCount ={0}; (* *)
For[k=1,k<Nflips,k++,
x=RandomReal[]; If[x > 0.95,{AppendTo[HeadsCount ,HeadsCount [[k]]-2]}, 
{AppendTo[HeadsCount ,HeadsCount [[k]] -4  ]}]] (* *)
ListLinePlot[Transpose[{flip,HeadsCount }]] (* *)

(e) Many Coin-Flip Simulations

Nsimulations=36; (* *)
Nflips=155; (* *)
flip = Range[0,Nflips-1]; (* *)
SimHeadsCount={}; (* *)
For[j=1,j<Nsimulations,j++,HeadsCount ={0};For[k=1,k<Nflips,k++,x=RandomReal[];If[x > 0.95,AppendTo[HeadsCount,HeadsCount[[k]]+3],AppendTo[HeadsCount,HeadsCount[[k]]+1]]];AppendTo[SimHeadsCount,HeadsCount[[Nflips] ]]]; (* *)

Histogram[SimHeadsCount,50] (* *)

N[Mean[SimHeadsCount]] (* *)

N[StandardDeviation[SimHeadsCount]](* *)

(f) Describing the Distribution

(* *)