> restart; > with(student): > > # here is the simpson's rule routine > Simp:= proc(F,a,b,n) > local i,xr, S, dx; > dx:= (b-a)/n; > S:=F(a)+F(b); > for i from 1 to (n-1) do > xr:=a+i*dx; > if (modp(i,2)=1) then > S:= S+4*F(xr); > else > S:= S+2*F(xr); > end if; > end do:# > S:=S*dx/3; > S; > end: > > # here is the midpoint rule: > Midp:= proc(F,a,b,n) > local i,xm, M, dx; > M:=0; > dx:= (b-a)/n; > for i from 1 to n do > xm:=a+(i-.5)*dx; > M:= M+F(xm); > end do: > M:=M*dx; > M; > end: > > > > # we can now use these, we just have to > # define a function and endpoint: > > F:= x->exp(-x^2); a:=0.0; b:=1.0;n:=10; 2 F := x -> exp(-x ) a := 0. b := 1.0 n := 10 > > > Midp(F,a,b,n); .7471308778 > Simp(F,a,b,n); .7468249487 > > > # or (using the student package), > # you can use maple's built in > # simpson's rule (which seems to work just as good as mine) > > evalf(simpson(exp(-x^2), x=0..1,10)); .7468249480 > > evalf(middlesum(exp(-x^2), x=0..1,10)); .7471308778 >