#pragma rtGlobals=1 // Use modern global access method. // Date: 011/12/2012 //Version 1.2 // Procedure for simulating a random walk to model Brownian Motion. A single random walk (frame) consists of some number of steps and results in a random mean displacement. // A series of these random displacements (frames) can then be used to estimate Brownian Motion for a particle in 2-D space. // Written by Josef G. Trapani (jtrapani@amherst.edu) and Ashley Carter (acarter@amherst.edu) at Amherst College //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// Menu "Macros" "Brownian Motion", Start_BM_Panel("start") End //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// Function Start_BM_Panel(ctrlName) String ctrlName String savedDF= GetDataFolder(1) // Remember CDF in a string. DoWindow BM_Panel0 If (V_flag!=0) KillWindow BM_Panel0 SetDataFolder root:BM DoWindow /K BM_histgraph1 DoWindow /K BM_histgraph2 DoWindow /K BM_graph1 DoWindow /K BM_graph2 DoWindow /K D_table1 KillWaves /Z /A KillDataFolder /Z root:BM Endif If (cmpstr(ctrlName,"start")==0) NewDataFolder/O/S root:BM Variable /G stepNum = 1000 Variable /G frameNum = 100 Variable /G trackNum = 1 SetDataFolder savedDF // and restore BM_Panel(ctrlName) Endif End //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// Function BM_Panel(ctrlName) String ctrlName String info = IgorInfo(0) String screen1RectStr = StringByKey("SCREEN1", info) //e.g., "DEPTH=23,RECT=0,0,1280,1024" Variable depth, left, top, right, bottom sscanf screen1RectStr, "DEPTH=%d,RECT=%d,%d,%d,%d", depth, left,top, right, bottom String platform= UpperStr(igorinfo(2)) If (cmpstr(platform,"WINDOWS")==0) NewPanel /N=BM_Panel0 /W=(right-300,top,right,200) /K=2 ///FLT=2 Else NewPanel /N=BM_Panel0 /W=(right-400,top,right-100,200) /K=2 ///FLT=2 Endif ModifyPanel /W=BM_Panel0 cbRGB=(62535,62535,62535), frameStyle=3, frameInset=5, noEdit=1, fixedSize=1 //Info and controls that should always be displayed; i.e. controls above the tabs SetDrawLayer /W=BM_Panel0 ProgFront DrawRRect /W=BM_Panel0 10,35,290,140 SetDrawEnv /W=BM_Panel0 fsize= 18, fillbgc=(49151,53155,65535) DrawText /W=BM_Panel0 40,70,"Brownian Motion Simulator" SetDrawEnv /W=BM_Panel0 fsize= 10, fillbgc=(49151,53155,65535) Button closeBMPanelbutton,pos={210,10},size={60,20},proc=Start_BM_Panel,title="Close", win=BM_Panel0 SetVariable BMsetvar0,pos={10,160},size={90,15},title="Steps:", win=BM_Panel0 SetVariable BMsetvar0,fSize=10,limits={1,100000,0},value= root:BM:stepNum, win=BM_Panel0 SetVariable BMsetvar1,pos={110,160},size={90,15},title="Frames:", win=BM_Panel0 SetVariable BMsetvar1,fSize=10,limits={1,10000,0},value= root:BM:frameNum, win=BM_Panel0 SetVariable BMsetvar2,pos={210,160},size={80,15},title="Tracks:", win=BM_Panel0 SetVariable BMsetvar2,fSize=10,limits={1,100,0},value= root:BM:trackNum, win=BM_Panel0 Button BMbutton0, pos={100,80}, size={120,40}, title="Start\rRandom Walk(s)", proc=runBM, fColor=(65535,54607,32768), win=BM_Panel0 End //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// Function runBM(ctrlName):ButtonControl String ctrlName NVAR numtracks= root:BM:trackNum If (numtracks==1) runBM1(numtracks) Else runBM2(numtracks) Endif End //////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// Function runBM1(tempNum) Variable tempNum Variable a,z,f NVAR numSteps= root:BM:stepNum NVAR numframes= root:BM:frameNum NVAR numtracks= root:BM:trackNum Variable elapsetime, ticktime, i String savedDF= GetDataFolder(1) // Remember CDF in a string. String BM_distances = "root:BM:BM_distances" String BM_XWave = "root:BM:BM_X" String BM_YWave = "root:BM:BM_Y" String BM_XYwave = "root:BM:BM_XYvals" String BMhistwave = "root:BM:BM_stdHist" String histwave = "root:BM:BM_distancesHist" //Make a wave to store each frame's displacement Make /O /N=0 $BM_distances Wave BM_distancesWave = $BM_distances Make /O /N=(numframes) $BM_XWave Wave BMX = $BM_XWave Make /O /N=(numframes) $BM_YWave Wave BMY = $BM_YWave String X_startsum = "root:BM:BM_startXvalues" String X_endsum = "root:BM:BM_endXvalues" Make /O /N=(numframes) $X_startsum Wave xstartsum = $X_startsum Make /O /N=(numframes) $X_endsum Wave xendsum = $X_endsum String Y_startsum = "root:BM:BM_startYvalues" String Y_endsum = "root:BM:BM_endYvalues" Make /O /N=(numframes) $y_startsum Wave ystartsum = $y_startsum Make /O /N=(numframes) $Y_endsum Wave yendsum = $y_endsum For (f=0; f 0+0+1+1+0.5+0.5+0.5+0.5 / 8 Variable Diff = fitVariance/(2*numSteps) SetDataFolder savedDF TextBox/C/N=textbox2/A=LT /X=0/Y=0 "D = "+num2str(Diff)+"\r\Z08Nominal D = "+num2str(0.5/2) If (tempNum==numtracks) //Histogram of displacements for each frame Make/N=100/O $histwave Variable binNum =sqrt(numSteps) Histogram /C /B=1 $BM_distances,$histwave //Display graph of displacements from each random walk taken (frame) Display /N=BM_histgraph2 /K=1 /W=(200,480,600,700 ) $histwave ModifyGraph mode=5,hbFill=2,rgb=(26214,26214,26214),useBarStrokeRGB=1 TextBox/C/N=textbox2/A=LT /X=0/Y=0 "Mean displacement length = "+num2str(Round(BM_avgLength))+"\r\Z08Histogram of displacements for "+num2str(numframes)+" frames." Display /N=BM_graph2 /K=1 /W=(600,50,1000,450 ) $Y_endsum vs $X_endsum ModifyGraph rgb=(0,0,0) ModifyGraph mirror=1 SetAxis/A/N=1/E=2 left SetAxis/A/N=1/E=2 bottom TextBox/C/N=textbox1/A=RB /X=0/Y=0 "Consecutive displacements for the "+num2str(numframes)+" frames" DoUpdate GetAxis left Variable tempMax1 = V_max GetAxis bottom Variable tempMax2 = V_max If (tempMax1