PH36010 Numerical Methods Some more useful functions in MathCAD Piecewise continuous functions • So far, functions have been continuous & ‘smooth’ • Real world has ‘abrupt’ changes • MathCAD has functions which can model these changes Abrupt changes in the real world • The impulse given by a hammer. • The magnetic field due to a current in a circular conductor has different equation inside and outside of the conductor. • A rocket motor produces a constant thrust until the fuel runs out, then it produces zero thrust. • A potential well has abrupt changes in potential at its edges. The if() function • • • • • • 3 arguments 1 boolean expression (condition) ‘True’ value ‘False’ value Condition evaluated if ‘true’ (non-zero) return true value else return false value Boolean Expressions • Use relation operators (evaluation palette) =,>,<,,, • True=1 (anything non-zero) • False=0 • Can use multiply & add for AND/OR Examples of if() if( 1 0 999 42) 999 if( 5 6 1 ) 1 Magnetic field due to wire #1 B B 0 I 2 R 0 I R 2 2 a Field outside wire (R>a) Field inside wire (R<a) Extract common terms & combine with if() BWireR ( ) 0 I test if R a 1 R 2 R a2 Magnetic field due to wire #2 I test 2 A 2 1 0 4 a mm BW ire( R)1 1 0 4 0 0 2 4 6 R mm 8 10 The Kronecker delta function: d(x,y) • Provides an impulse. • Returns 1 if x=y, 0 otherwise • Equivalent to if(x=y,1,0) x 10 9.8 10 . 1 d ( x 2 ) 0 1 0 2 4 6 x 8 10 Heavyside Step function F(x) • Provides a step at x=0 • Equivalent to if(x>0,1,0) 2 2 F ( x 1) 1 0 0 10 10 5 0 x 5 10 10 Combining F(x) to form pulses t1 1 pulse( x) t2 3 F (x t1 ) F ( t2 x) 2 t1 p ulse( x) t2 1 0 0 2 4 6 x 8 10 Truncation floor(), ceil(), trunc() • All functions take real & convert to integer • trunc 0 • floor - • ceil tru nc3.1 ( ) 3 tru nc3.9 ( ) 3 tru nc( 3.1) 3 tru nc( 3.9) 3 flo or( 3.1) 3 flo or( 3.9) 3 flo or( 3.1) 4 flo or( 3.9) 4 ceil( 3.1) 4 ceil( 3.9) 4 ceil( 3.1) 3 ceil( 3.9) 3 Random Numbers • Useful for: – Modelling statistical processes – Monte Carlo simulation • 2 methods of generation: – rnd(x) – returns 1 number in range 0<x (Uniform distribution) – All others return vector of numbers with specified distribution Random Numbers • Lottery Predictor (!!) NBalls i 6 MaxNum 42 0 NBalls 1 Drawi trunc( rnd( 42 ) ) 1 T Draw ( 13 29 18 6 20 3 ) • Needs work • Duplicate numbers • Not guaranteed