# MCS471 Trapezoidal and Simpson's Rules Example Maple Worksheet ------------------------------ ------------------------------ # Sample User Defined Function: # Related to Normal Probability Distribution by Scaling: ------------------------------ > f:=x->exp(-x^2); 2 f := x -> exp(- x ) ------------------------------ # Remark: Maple Numerical Integration Functions # Require Maple Student Calculus Package ------------------------------ > with(student): ------------------------------ # Maple Student Function Trapezoidal Rule has format: trapezoid("Function(x)",x="A".."B"[,NumberSubintervals"]); # Remark: The number of subintervals is optional. # Below, "A = 0", "B = 1" and "n = 20" subintervals for above user defined # function. ------------------------------ > trapezoid(f(x),x=0..1,20); / 19 \ |----- | | \ 2 | 1/40 + 1/20 | ) exp(- 1/400 i )| + 1/40 exp(-1) | / | |----- | \i = 1 / ------------------------------ # Remark: See Maple Language Help for more examples. # The "trapezoid" function first the discrete formula. # Maple "evalf" produces 10 digit floating point value. # Note: "n = 20" subintervals means "n+1 = 21" points or function evaluations. ------------------------------ > TR21:=evalf("); TR21 := .7466708370 ------------------------------ # Maple Student Function Simpson's (1/3) Rule has format: simpson("Function(x)",x="A".."B"[,NumberSubintervals"]); # Remark: The number of subintervals is optional. # Below, "A=0", "B=1" and "n=20" subintervals for above user defined # function. ------------------------------ # CAUTION: The Maple "simpson" also needs s "with(student):" statement above. ------------------------------ > simpson(f(x),x=0..1,20); / 10 \ |----- | | \ 2 | 1/60 + 1/60 exp(-1) + 1/15 | ) exp(- (1/10 i - 1/20) )| | / | |----- | \i = 1 / / 9 \ |----- | | \ 2 | + 1/30 | ) exp(- 1/100 i )| | / | |----- | \i = 1 / > SR21:=evalf("); SR21 := .7468241839 ------------------------------ # Remark: Check Relative Difference of Trapezaoidal to Simpson's Rule: ------------------------------ > RelDiff:=(TR21/SR21-1)*100; RelDiff := -.02053320 ------------------------------ # Remark: Hence a 0.02% difference of Trapezaoidal to Simpson's Rule: ------------------------------ # ------------------------------ # Remark: Next, we try 40 subintervals or 41 points (function evaluations). ------------------------------ > TR41:=evalf(trapezoid(f(x),x=0..1,40),25); TR41 := .7467858112389793003907348 ------------------------------ # Remark: In "evalf" function, 25 digits were requested in the last, optional # argument. ------------------------------ > SR41:=evalf(simpson(f(x),x=0..1,40),25); > RelDiff41:=(TR41/SR41-1)*100; SR41 := .7468241360053479327763032 RelDiff41 := -.00513170 # Remark: Now the Relative Difference is 0.005%, # consistent with ratio of Simpson's to trapezoidal theoretical errors # of O(h^2) which becomes O(h^2/4) upon bisecting the step size "h".