PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

22
PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD

Transcript of PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Page 1: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

PH24010Data Handling and Statistics

Photon Scattering program written in MathCAD

Page 2: 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

Page 3: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

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

Page 4: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

MathCAD programsif statement

• Better than if() function for complicated cases.

• otherwise statement to catch unhandled cases.

Page 5: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Programmed if statement

fThrust t( ) 0 t 0if

100 0 t 5if

80 5 t 20if

0 otherwise

• Note: – Comparisons– use of otherwise to

catch all cases

Page 6: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

MathCAD programs – The for loop

fOnes N( )

Resulti 1

i 0 N 1for

Result

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

iterations

Page 7: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

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

Page 8: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

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

Thresh t Vec( ) j 0

j j 1

Vecj twhile

j

Vecj

Page 9: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

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

Page 10: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Longer Loops

fCatInHat x( )

UpUpUpWithAFish

MotherIsOut x( )while

fCatInHat2 x( )

Thing1

Thing2

MotherIsOut x( )while

Page 11: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Program ExamplePhoton 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

Page 12: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Photon ScatteringProgram to create Walk

OnePath InitX InitY Init i 0

x InitX

y InitY

Resulti 0 x

Resulti 1 y

P PathLength0( )

ScatterAngle Init

x x P cos

y y P sin

i i 1

InBox x y( )while

Resulti 0 x

Resulti 1 y

Result

Page 13: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Program ExamplePhoton Scattering #2

• Store x-y co-ordinates and i (loop count)• Write functions for

– Pathlength()– ScatterAngle()– InBox(x,y)

• Test these functions !!!

Page 14: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Photon ScatteringPathLength 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

PathLengthx( ) rexp 1 path 0

Page 15: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Photon ScatteringScatterAngle()

• Isotropic scatter - uniform• Give 1 angle randomly between – • Similar use of built-in random numbers to

earlier.• Deal with anisotropy later

ScatterAngle runif 1 0

Page 16: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

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

MinXBoxSize2

MaxXBoxSize

2 MinY

BoxSize2

MaxYBoxSize

2

InBox x y( ) MinX x MaxX( ) MinY y MaxY( )

Page 17: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Photon ScatteringProgram to create Walk

OnePath InitX InitY Init i 0

x InitX

y InitY

Resulti 0 x

Resulti 1 y

P PathLength0( )

ScatterAngle Init

x x P cos

y y P sin

i i 1

InBox x y( )while

Resulti 0 x

Resulti 1 y

Result

Page 18: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Photon ScatteringUsing the programHaving created the data in MyFirstWalk, we can now plot the path taken by our photon on an

X-Y Graph

MyFirstWalk OnePath 0 0 0( ) rows MyFirstWalk( ) 509

400 200 0 200 400500

0

500

MyFirstWalk1

MyFirstWalk0

Page 19: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Photon ScatteringConclusions

• 14 line program + functions

• Records entire walk

• Extract info from result vector

• Easy to extend– 3D scatter– Anisotropy

• Change ScatterAngle()

Page 20: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Combining Many Walks

• Use stack() to join results together

ManyWalks InitX InitY Init NWalk Result OnePath InitX InitY Init

Result stack Result OnePath InitX InitY Init

i 1 NWalk 1for

Result

Page 21: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Simple anisotropy

• Assume ‘normal’ distribution about angle

0.2

ScatterAngle rnorm 1 0

Page 22: PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.

Many walks with anisotropy

400 200 0 200 400500

0

500MaxY

MinY

ManyPaths 1

MaxXMinX ManyPaths 0