1 / 36

PH36010 Numerical Methods

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.

malo
Download Presentation

PH36010 Numerical Methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PH36010 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. 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 ProgramsOnline 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 ProgramsStructures • 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 programsif 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 ProgramsHistogram Example #1 • Without programming • Calculate number & width of bins • Create Bin array • Create histogram Need to write these steps for every histogram

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

  26. MathCAD ProgramsUsing the Hist() function • Same as any other function

  27. 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

  28. Photon ScatteringProgram to create Walk

  29. 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 !!!

  30. 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

  31. Photon ScatteringScatterAngle(q) • Isotropic scatter - uniform • All angles between –pp • Deal with anisotropy later

  32. Photon ScatteringInBox(V) function • Takes Vector as argument • ‘Unpacks’ argument with subscripts • Uses multiplication to form AND • Returns 1 if in box, 0 otherwise

  33. Photon ScatteringNewPos(V,P,q) • Arguments • OldPosition V • Pathlength P • Angle q • Returns new position as vector

  34. Photon ScatteringProgram to create Walk

  35. Photon ScatteringUsing the program

  36. Photon ScatteringConclusions • 8 line program + functions • Records entire walk • Extract info from result vector • Easy to extend • 3D scatter • Anisotropy • Change ScatterAngle(q)

More Related