# This is a code for 8.08 - 8.S421, 1st-2nd recitation. # You will run simulations of a Brownian particle in harmonic potential # Compare how the outcome varies when you change the simulation time step and the algorithm implemented. # parse command line argument(s) ((length(ARGS)==1) && (ARGS[1] in ["1","2"])) || throw(ArgumentError("Argument 1 must be either 1 (Euler/Maruyama) or 2 (Mannella)")) # for installing required packages # using Pkg # Pkg.add("Plots") # Pkg.add("Random") # Pkg.add("LsqFit") # load packages using Plots using Random using LsqFit # integrator for simulating a harmonic oscillator function runHarmonicOscillator!( tf, # duration of your simulation dt, # time step seed, # seed for random number generator resolution, # resolution of your historgram width, # width of your histogram num_cells, # number of cells in your histogram num_record, # number of data to be recorded data_histogram # array for recording time ) rng = MersenneTwister(seed) x_now, x_next = 0, 0 #state variables #### You might want to define more quantities or not if ARGS[1]=="1" a = 1-dt b = sqrt(2*dt) elseif ARGS[1]=="2" a = 1-dt+dt*dt/2 b = sqrt(2*dt*(1-dt+dt*dt/3)) end #### t = 0 # current time Delta_t = tf / num_record # sampling time gap record_index = 1 # current index of my recording fill!(data_histogram, 0) # initialize your histogram while t < tf + Delta_t # simulate until tf is reached, with a tiny margin to assure full recording #### #### #### WRITE YOUR EQUATIONS OF MOTION HERE x_next = a*x_now + b*randn(rng) x_now = x_next #### #### #### if t > record_index*Delta_t && record_index