1

 Photon Scattering program written in MathCAD

2

 Applies to all programming
 K.I.S.S. principle

3

 if, otherwise, for, while
 Indentation & vertical bars
 Watch selection rectangle
 <Space> to increase (more lines)
 <Insert> to swap sides
 No GOTO

4

 Better than if() function for complicated cases.
 otherwise statement to catch unhandled cases.

5

 Note:
 Comparisons
 use of otherwise to catch all cases

6

 Loop extend shown by indent
 ‘Result’ array built up
 Note syntax of ‘for’ line
 Use when you know in advance how many iterations

7

 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

8

 Find first member of vector ‘Vec’ greater than threshold, ‘t’
 Written as function
 j is index
 while loop
 return index & value as vector

9

 Use ‘Add Line’ in body of loop to extend scope of loop.
 Lines added at vertical bar
 <Insert> key swaps sides of selection bar

10


11

 Photon enters box
 Travels random distance
 Scatter through random angle
 Repeat from step 2 until photon leaves box
 Record walk for posterity

12


13

 Store xy coordinates and i (loop count)
 Write functions for
 Pathlength()
 ScatterAngle(q)
 InBox(x,y)
 Test these functions !!!

14

 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

15

 Isotropic scatter  uniform
 Give 1 angle randomly between –p ® p
 Similar use of builtin random numbers to earlier.
 Deal with anisotropy later

16

 Takes x,y as arguments
 2 way logical expression
 Uses multiplication to form AND
 Returns 1 if in box, 0 otherwise

17


18


19

 14 line program + functions
 Records entire walk
 Extract info from result vector
 Easy to extend

20

 Use stack() to join results together

21

 Assume ‘normal’ distribution about angle

22

