MathCAD - Programming & Interpolation

advertisement
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
– Simple
– Stupid
MathCAD Programs
Structures
• if, otherwise, for, while
– Indentation & vertical bars
– Watch selection rectangle
– <Space> to increase (more lines)
– <Insert> 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:
fTh ru stt
( ) 
0 if t  0
10 0 i f 0  t  5
80 i f 5  t  20
0 oth erwise
– Comparisons
– use of otherwise to
catch all cases
MathCAD programs – The for
loop
fOn es( N)  
for i  0  N  1
Res uli t 1
Res ul t
•
•
•
•
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
Th res ht
(  Vec)
j
0
wh il e Vecj t
j
j
j
Vecj
1
• 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
Longer Loops
fCatInHat( x)  while MotherIsOut ( x)
UpUpUpWithAFish
fCatInH at (2x)   wh il e Mo th erIsOu
( xt)
Th i ng1
Th i ng2
Program Example
Photon Scattering #1
1.
2.
3.
4.
Photon enters box
Travels random distance
Scatter through random angle
Repeat from step 2 until photon leaves
box
5. Record walk for posterity
OnePath InitX InitY Init  
i 0
x  InitX
y  InitY
while InBox( x  y)
Resulti  0  x
Resulti  1  y
P  PathLength( 0)
  ScatterAngle Init
Photon
Scattering
Program to
create Walk
x  x  P cos   
y  y  P sin  
i i 1
Resulti  0  x
Resulti  1  y
Result
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
P ath Leng(th
x)  rexp 1  spath 0
• 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 Scattering
ScatterAngle()
   run if1
  p  p  0
ScatterAn gle
• Isotropic scatter - uniform
• Give 1 angle randomly between –p  p
• Similar use of built-in random numbers to
earlier.
• Deal with anisotropy later
Photon Scattering
InBox(V) function
MinX 
BoxSize
2
MaxX 
BoxSize
2
MinY 
BoxSize
2
InBox( x  y)  ( MinX  x  MaxX) ( MinY  y  MaxY)
• Takes x,y as arguments
• 2 way logical expression
– LoLimit < x < HiLimit
• Uses multiplication to form AND
• Returns 1 if in box, 0 otherwise
MaxY 
BoxSize
2
OnePath InitX InitY Init  
i 0
x  InitX
y  InitY
while InBox( x  y)
Resulti  0  x
Resulti  1  y
P  PathLength( 0)
  ScatterAngle Init
Photon
Scattering
Program to
create Walk
x  x  P cos   
y  y  P sin  
i i 1
Resulti  0  x
Resulti  1  y
Result
Photon Scattering
Using the program
Having created the data in MyFirstWalk, we can now plot the path taken by our photon on an
X-Y Graph
My FirstWalk  OnePath( 0  0  0)
rows( My FirstWalk)  509
500
 
My FirstWalk1
0
500
400
200
0
 
My FirstWalk0
200
400
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
Many Walks InitX InitY Init  NWalk  Result  OnePath InitX InitY Init
for i 1  NWalk  1
Result  stack Result  OnePath InitX InitY Init 
Result
Simple anisotropy
• Assume ‘normal’ distribution about angle
  0.2
   rno rm1
     0
ScatterAn gle
Many walks with anisotropy
500
MaxY
 
Many Paths1
0
MinY
500
400
MinX
200
0
 
Many Paths0
200
400
MaxX
Download