220 likes | 316 Views
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 K eep I t S imple S tupid. MathCAD Programs Structures. if, otherwise, for, while Indentation & vertical bars
E N D
PH24010Data 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 • Simple • Stupid
MathCAD ProgramsStructures • if, otherwise, for, while • Indentation & vertical bars • Watch selection rectangle • <Space> to increase (more lines) • <Insert> to swap sides • No GOTO • Considered harmful
MathCAD programsif 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 • <Insert> key swaps sides of selection bar
Program ExamplePhoton Scattering #1 • Photon enters box • Travels random distance • Scatter through random angle • Repeat from step 2 until photon leaves box • Record walk for posterity
Program ExamplePhoton Scattering #2 • Store x-y co-ordinates and i (loop count) • Write functions for • Pathlength() • ScatterAngle(q) • InBox(x,y) • Test these functions !!!
Photon ScatteringPathLength function • spath is related to ln(2)/mean path • x placeholder is dummy • rexp(1, spath) function returns vector of 1 number from distribution • indexing to extract element 0 from vector
Photon ScatteringScatterAngle(q) • Isotropic scatter - uniform • Give 1 angle randomly between –pp • Similar use of built-in random numbers to earlier. • Deal with anisotropy later
Photon ScatteringInBox(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 ScatteringConclusions • 14 line program + functions • Records entire walk • Extract info from result vector • Easy to extend • 3D scatter • Anisotropy • Change ScatterAngle(q)
Combining Many Walks • Use stack() to join results together
Simple anisotropy • Assume ‘normal’ distribution about angle