ph36010 numerical methods l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
PH36010 Numerical Methods PowerPoint Presentation
Download Presentation
PH36010 Numerical Methods

Loading in 2 Seconds...

play fullscreen
1 / 36

PH36010 Numerical Methods - PowerPoint PPT Presentation


  • 430 Views
  • Uploaded on

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.

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

PowerPoint Slideshow about 'PH36010 Numerical Methods' - malo


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

PH36010 Numerical Methods

Writing Programs in MathCAD

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
The Golden Rule of Programming
  • Applies to all programming
  • K.I.S.S. principle
    • Keep
    • It
    • Simple
    • Stupid
a mathcad program
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
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
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
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
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 programs online help
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
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
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 programs structures
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 programs if statement
MathCAD programsif statement
  • Better than if() function for complicated cases.
  • otherwise statement to catch unhandled cases.
if statement in programs
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
Programmed if statement
  • Note:
    • Comparisons
    • use of otherwise to catch all cases
mathcad programs the for loop
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
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
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
Using our Thresh function
  • Vv is test vector
  • Function returns index and value
longer loops
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 programs histogram example 1
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 programs histogram example 2
MathCAD ProgramsHistogram Example #2
  • As a program
  • Returns 2 element vector
    • Bin
    • Histogram
  • Min, Max, i local
  • Written as a function
program example photon scattering 1
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 example photon scattering 2
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 scattering pathlength function
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 scattering scatterangle q
Photon ScatteringScatterAngle(q)
  • Isotropic scatter - uniform
  • All angles between –pp
  • Deal with anisotropy later
photon scattering inbox v function
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 scattering newpos v p q
Photon ScatteringNewPos(V,P,q)
  • Arguments
    • OldPosition V
    • Pathlength P
    • Angle q
  • Returns new position as vector
photon scattering conclusions
Photon ScatteringConclusions
  • 8 line program + functions
  • Records entire walk
  • Extract info from result vector
  • Easy to extend
    • 3D scatter
    • Anisotropy
      • Change ScatterAngle(q)