PH36010 Numerical Methods

1 / 36

# PH36010 Numerical Methods - PowerPoint PPT Presentation

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'PH36010 Numerical Methods' - malo

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### PH36010 Numerical Methods

• 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
• All MathCAD programs are functions
• Expression over multiple lines
• Lines executed in order
• Local Variables
• Value of last line is result
• 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
• Programming Palette from toolbar
• Select keywords from palette, DO NOT TYPE
• Watch selection box, carefully
• 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
• Help|Resource Centre
• 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
• if, otherwise, for, while
• Indentation & vertical bars
• Watch selection rectangle
• <Space> to increase (more lines)
• <Insert> to swap sides
• No GOTO
• Considered harmful
• 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
• Without programming
• Calculate number & width of bins
• Create Bin array
• Create histogram

Need to write these steps for every

histogram

• As a program
• Returns 2 element vector
• Bin
• Histogram
• Min, Max, i local
• Written as a 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
Photon ScatteringScatterAngle(q)
• Isotropic scatter - uniform
• All angles between –pp
• 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)