1

 Writing Programs in MathCAD

2

 Can do much without programming
 Some algorithms need programs
 Iterative, repeat until solution
 Can make other tasks simpler
 Hide detail inside a program

3

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

4

 All MathCAD programs are functions
 Expression over multiple lines
 Lines executed in order
 Local Variables
 Value of last line is result

5

 Common subexpression calculated 3 times in function
 è Can rewrite
using a program

6

 1^{st} line calculates sqrt & assigns to local variable ‘r’
 2^{nd} line calculates expression & returns answer

7

 Programming Palette from toolbar
 Select keywords from palette, DO NOT TYPE
 Add Line to add lines to program
 Watch selection box, carefully

8

 Assignments are local to program
 Assignments in program use ¬
 Can use full range of MathCAD functions in
program
 Last line is result of program
 Use Vectors & Arrays to return multiple results

9

 HelpResource Centre
 Advanced TopicsTreasury Guide to ProgrammingProgramming Within
MathCAD
 QuicksheetsProgramming

10

 Equations for r and q given x & y
 Want 1 function to return both values
 è Use a vector

11

 Function returns 2 element vector
 Element 0 è r
 Element 1 è q
 NB. Could use atan2(x,y)
No error for x=0

12

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

13

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

14

 Model rocket engine
 Thrust = 0 for t<0
 Thrust = 100 for t>0 and t<5
 Thrust = 80 for t>5 and t<20
 Thrust = 0 for t>20

15

 Note:
 Comparisons
 use of otherwise to catch all cases

16


17

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

18


19

 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

20

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

21

 Vv is test vector
 Function returns index and value

22

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

23


24

 Without programming
 Calculate number & width of bins
 Create Bin array
 Create histogram

25

 As a program
 Returns 2 element vector
 Min, Max, i local
 Written as a function

26

 Same as any other function

27

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

28


29

 Store xy coordinates as 2 element vectors V
 Write functions for
 Pathlength()
 ScatterAngle(q)
 InBox(V)
 NewPos(V,P,q)
 Test these functions !!!

30

 s is related to 1/mean path
 x placeholder is dummy
 rnd(1) gives random number 01
 See also rexp(NPts,1/s) function

31

 Isotropic scatter  uniform
 All angles between –p ®
p
 Deal with anisotropy later

32

 Takes Vector as argument
 ‘Unpacks’ argument with subscripts
 Uses multiplication to form AND
 Returns 1 if in box, 0 otherwise

33

 Arguments
 OldPosition V
 Pathlength P
 Angle q
 Returns new position as vector

34


35


36

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