370 likes | 911 Views
PH36010 Numerical Methods. Writing Programs in MathCAD . Writing Programs in MathCAD. Can do much without programming Some algorithms need programs Iterative, repeat until solution Can make other tasks simpler Hide detail inside a program. The Golden Rule of Programming.
E N D
PH36010 Numerical Methods Writing Programs in MathCAD
Writing Programs in MathCAD • Can do much without programming • Some algorithms need programs • Iterative, repeat until solution • Can make other tasks simpler • Hide detail inside a program
The Golden Rule of Programming • Applies to all programming • K.I.S.S. principle • Keep • It • Simple • Stupid
A MathCAD program • All MathCAD programs are functions • Expression over multiple lines • Lines executed in order • Local Variables • Value of last line is result
A simple MathCAD program #1 • Common subexpression calculated 3 times in function • Can re-write using a program
fExample(a,b) as a 2 line program • 1st line calculates sqrt & assigns to local variable ‘r’ • 2nd line calculates expression & returns answer
MathCAD Programs #1 • Programming Palette from toolbar • Select keywords from palette, DO NOT TYPE • Add Line to add lines to program • Watch selection box, carefully
MathCAD Programs #2 • 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
MathCAD ProgramsOnline Help • Help|Resource Centre • Advanced Topics|Treasury Guide to Programming|Programming Within MathCAD • Tutorial • Quicksheets|Programming • Reference • Examples
Rectangular to Polar Conversion #1 • Equations for r and q given x & y • Want 1 function to return both values • Use a vector
Rectangular to Polar Conversion • Function returns 2 element vector • Element 0 r • Element 1 q • NB. Could use atan2(x,y) No error for x=0
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.
If statement in programs • 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
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
Using our Thresh function • Vv is test vector • Function returns index and value
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
MathCAD ProgramsHistogram Example #1 • Without programming • Calculate number & width of bins • Create Bin array • Create histogram Need to write these steps for every histogram
MathCAD ProgramsHistogram Example #2 • As a program • Returns 2 element vector • Bin • Histogram • Min, Max, i local • Written as a function
MathCAD ProgramsUsing the Hist() function • Same as any other function
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 as 2 element vectors V • Write functions for • Pathlength() • ScatterAngle(q) • InBox(V) • NewPos(V,P,q) • Test these functions !!!
Photon ScatteringPathLength function • s is related to 1/mean path • x placeholder is dummy • rnd(1) gives random number 0-1 • See also rexp(NPts,1/s) function
Photon ScatteringScatterAngle(q) • Isotropic scatter - uniform • All angles between –pp • Deal with anisotropy later
Photon ScatteringInBox(V) function • Takes Vector as argument • ‘Unpacks’ argument with subscripts • Uses multiplication to form AND • Returns 1 if in box, 0 otherwise
Photon ScatteringNewPos(V,P,q) • Arguments • OldPosition V • Pathlength P • Angle q • Returns new position as vector
Photon ScatteringConclusions • 8 line program + functions • Records entire walk • Extract info from result vector • Easy to extend • 3D scatter • Anisotropy • Change ScatterAngle(q)