PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD
The Golden Rule of Programming Applies to all programming K.I.S.S. principle –Keep –It–It –Simple –Stupid
MathCAD Programs Structures if, otherwise, for, while –Indentation & vertical bars –Watch selection rectangle – to increase (more lines) – to swap sides No GOTO –Considered harmful
MathCAD programs if statement Better than if() function for complicated cases. otherwise statement to catch unhandled cases.
Programmed if statement Note: –Comparisons –use of otherwise to catch all cases
MathCAD programs – The for loop Loop extend shown by indent ‘Result’ array built up Note syntax of ‘for’ line Use when you know in advance how many iterations
The while loop Execute statements while a condition is true Used when you don’t know in advance how many times loop will be executed. Loop while you are searching Loop while error is too big Loop while system is stable
A while loop example Find first member of vector ‘Vec’ greater than threshold, ‘t’ Written as function j is index while loop return index & value as vector
Longer Loops Use ‘Add Line’ in body of loop to extend scope of loop. Lines added at vertical bar key swaps sides of selection bar
Longer Loops
Program Example Photon Scattering #1 1.Photon enters box 2.Travels random distance 3.Scatter through random angle 4.Repeat from step 2 until photon leaves box 5.Record walk for posterity
Photon Scattering Program to create Walk
Program Example Photon Scattering #2 Store x-y co-ordinates and i (loop count) Write functions for –Pathlength() –ScatterAngle( ) –InBox(x,y) Test these functions !!!
Photon Scattering PathLength function path is related to ln(2)/mean path x placeholder is dummy rexp(1, path ) function returns vector of 1 number from distribution indexing to extract element 0 from vector
Photon Scattering ScatterAngle( ) Isotropic scatter - uniform Give 1 angle randomly between – Similar use of built-in random numbers to earlier. Deal with anisotropy later
Photon Scattering InBox(V) function Takes x,y as arguments 2 way logical expression –LoLimit < x < HiLimit Uses multiplication to form AND Returns 1 if in box, 0 otherwise
Photon Scattering Program to create Walk
Photon Scattering Using the program
Photon Scattering Conclusions 14 line program + functions Records entire walk Extract info from result vector Easy to extend –3D scatter –Anisotropy Change ScatterAngle( )
Combining Many Walks Use stack() to join results together
Simple anisotropy Assume ‘normal’ distribution about angle
Many walks with anisotropy