#!/usr/bin/python -Wall # ================================================================ # John Kerl # kerl at math dot arizona dot edu # 2008-05-12 # ================================================================ from __future__ import division # 1/2 = 0.5, not 0. import random from math import * import copy import sys import re # ---------------------------------------------------------------- def usage(): print >> sys.stderr, "Usage: %s [options]" % (sys.argv[0]) print >> sys.stderr, "Options:" print >> sys.stderr, " T=[number]" print >> sys.stderr, " nt=[number]" print >> sys.stderr, " A=[number]" print >> sys.stderr, " B=[number]" print >> sys.stderr, " nX=[number]" sys.exit(1) # ---------------------------------------------------------------- # SDE: dY_t = (B - Y_t)/(1 - t) dt + db_t T = 1 nt = 1000 A = 0 B = 0 nX = 20 argc = len(sys.argv) for argi in range(1, argc): if re.match(r'^T=', sys.argv[argi]): T = float(sys.argv[argi][2:]) elif re.match(r'^nt=', sys.argv[argi]): nt = int (sys.argv[argi][3:]) elif re.match(r'^A=', sys.argv[argi]): A = float(sys.argv[argi][2:]) elif re.match(r'^B=', sys.argv[argi]): B = float(sys.argv[argi][2:]) elif re.match(r'^nX=', sys.argv[argi]): nX = int (sys.argv[argi][3:]) else: usage() X0 = A t = 0 dt = T/nt Bt = [0.0] * nX approx = [0.0] * nX sqrtdt = sqrt(dt) while (t < T): print "%11.7f" % (t), for k in range(0, nX): print "%11.7f" % (approx[k]), print for k in range(0, nX): dB = random.gauss(0, sqrtdt) approx[k] += (B - approx[k]) / (1 - t) * dt + dB Bt[k] += dB t += dt