Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Programs in MathCAD

Similar presentations


Presentation on theme: "Writing Programs in MathCAD"— Presentation transcript:

1 Writing Programs in MathCAD
PH Numerical Methods Writing Programs in MathCAD

2 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

3 The Golden Rule of Programming
Applies to all programming K.I.S.S. principle Keep It Simple Stupid

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

5 A simple MathCAD program #1
Common subexpression calculated 3 times in function  Can re-write using a program

6 fExample(a,b) as a 2 line program
1st line calculates sqrt & assigns to local variable ‘r’ 2nd line calculates expression & returns answer

7 Watch selection box, carefully
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

8 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

9 MathCAD Programs Online Help
Help|Resource Centre Advanced Topics|Treasury Guide to Programming|Programming Within MathCAD Tutorial Quicksheets|Programming Reference Examples

10 Rectangular to Polar Conversion #1
Equations for r and q given x & y Want 1 function to return both values  Use a vector

11 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

12 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

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

14 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

15 Programmed if statement
Note: Comparisons use of otherwise to catch all cases

16 fThrust() function

17 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

18 Using fOnes() program

19 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

20 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

21 Using our Thresh function
Vv is test vector Function returns index and value

22 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

23 Longer Loops

24 MathCAD Programs Histogram Example #1
Without programming Calculate number & width of bins Create Bin array Create histogram Need to write these steps for every histogram

25 MathCAD Programs Histogram Example #2
As a program Returns 2 element vector Bin Histogram Min, Max, i local Written as a function

26 MathCAD Programs Using the Hist() function
Same as any other function

27 Calculating the Fibbonaci series

28 Program Example Photon Scattering #1
Photon enters box Travels random distance Scatter through random angle Repeat from step 2 until photon leaves box Record walk for posterity

29 Photon Scattering Program to create Walk

30 Program Example Photon 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 !!!

31 Photon Scattering PathLength 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

32 Photon Scattering ScatterAngle(q)
Isotropic scatter - uniform All angles between –p  p Deal with anisotropy later

33 Photon Scattering InBox(V) function
Takes Vector as argument ‘Unpacks’ argument with subscripts Uses multiplication to form AND Returns 1 if in box, 0 otherwise

34 Photon Scattering NewPos(V,P,q)
Arguments OldPosition V Pathlength P Angle q Returns new position as vector

35 Photon Scattering Program to create Walk

36 Photon Scattering Using the program

37 Photon Scattering Conclusions
8 line program + functions Records entire walk Extract info from result vector Easy to extend 3D scatter Anisotropy Change ScatterAngle(q)


Download ppt "Writing Programs in MathCAD"

Similar presentations


Ads by Google