Math 151, Fall 2013

Riemann Sums, Antiderivatives, Integrals, FTC1

Riemann Sums

The following allows you to play with Riemann sums of some functions on the interval [0,2].

Manipulate[
(* a = 0, b = 2, Delta_u = (b-a)/n = 2/n *)
(* u[[0]],u[[1]],...,u[[n]] are the subinterval endpoints (instead of x0,x1,...,xn) *)
u=Range[0,2,2/n];

(* ustar[[0]],ustar[[1]],... are the sample points *)
ustar = Table[u[[k]]+(2/n)*RandomReal[],{k,1,Length[u]-1}];

(*piecewise defined approximating functions*)
leftstepfunc=Piecewise[
Table[{g[u[[k]]],u[[k]]<=x<=u[[k+1]]},{k,1,Length[u]-1}]];
midstepfunc=Piecewise[
Table[{g[(u[[k]]+u[[k+1]])/2],u[[k]]<=x<=u[[k+1]]},{k,1,Length[u]-1}]];
rightstepfunc=Piecewise[
Table[{g[u[[k+1]]],u[[k]]<=x<=u[[k+1]]},{k,1,Length[u]-1}]];
randstepfunc=Piecewise[
Table[{g[ustar[[k]]],u[[k]]<=x<=u[[k+1]]},{k,1,Length[u]-1}]];
approxgraph[x_]:=Switch[method,"left",leftstepfunc,"right",rightstepfunc,"midpoint",midstepfunc,"random", randstepfunc];

(*area approximation calculations*)
leftarea=N[Sum[g[u[[i]]]*(2/n),{i,1,Length[u]-1}]];
rightarea=N[Sum[g[u[[i+1]]]*(2/n),{i,1,Length[u]-1}]];
midarea=N[Sum[g[(u[[i]]+u[[i+1]])/2]*(2/n),{i,1,Length[u]-1}]];
randarea=N[Sum[g[ustar[[i]]]*(2/n),{i,1,Length[u]-1}]];
approxarea=Switch[method,"left",leftarea,"right",rightarea,"midpoint",midarea,"random", randarea];
exactarea=N[Integrate[g[x],{x,0,2}]];

(*points on curve used in each method*)
endpt=Table[{u[[k]],g[u[[k]]]},{k,1,Length[u]}];
endptline=Table[{{u[[k]],0},{u[[k]],g[u[[k]]]}},{k,1,Length[u]}];
midpt=Table[{(u[[k]]+u[[k+1]])/2,g[(u[[k]]+u[[k+1]])/2]},{k,1,Length[u]-1}];
randpt=Table[{ustar[[k]],g[ustar[[k]]]},{k,1,Length[u]-1}];
dots=ListPlot[Switch[method,"left",endpt,"right",endpt,"midpoint",midpt,"random", randpt]];
segments=ListLinePlot[Switch[method,"left",endptline,"right",endptline,"midpoint", endptline, "random",endptline],PlotRange->All];

(*graphics to Manipulate*)
Grid[
(*plots - first row*)
{{Show[{
Plot[{g[x],approxgraph[x]},{x,0,2},Filling->Axis,PlotPoints->300,PlotRange->{0,Automatic}],
dots,
segments,
Graphics[Inset[" ",{0,0}]]
},ImageSize->{550,300}]},


(*text - second row*)
{Framed@Text@Column[{
Style[Row[{"(∫_0^2)",TraditionalForm[g[x]],"",Style["dx",Italic]}],Black,16],
Row[{
Style[Row[{"approximate area = ",NumberForm[approxarea,{6,5}]}],Purple,16],
"    ",
Style[Row[{"exact area = ",NumberForm[exactarea,{6,5}]}],Blend[{Gray,Blue},1/2],16]
}],
Style[Row[{"|error| = ",ScientificForm[Abs[approxarea-exactarea],3]}],16]
},Alignment->Center]
}
}],

(*parameters for Manipulate*)
{{g,((2/(#^2+1))&),"function"},
{Sin->TraditionalForm[Sin[x]],
((2*#+1)&)->TraditionalForm[2*x+1],
((#^2)&)->TraditionalForm[x^2],
((Exp[-2*#])&)->TraditionalForm[Exp[-2*x]],
((5*#^(1/3))&)->TraditionalForm[5*x^(1/3)],
((2/(#^2+1))&)->TraditionalForm[2/(x^2+1)]},
ControlType->PopupMenu},
{{n,22,"subintervals"},2,40,2,Appearance->"Labeled"},
{{method,"left","sample points"},{"left","right","midpoint","random"}},
AutorunSequencing->{2,3,4},
TrackedSymbols->True]

The “approximate area” is the sum of the area of the rectangles: this is a Riemann Sum.  (Whether the sample points are left endpoints of the subintervals, right endpoints of the subintervals, midpoints, or any points within the subintervals, it is still a Riemann Sum.)

The “exact area” is the area under the curve: this is the integral.

The “error” is the difference between the Riemann Sum and the Integral.  
It is written in scientific notation.  For example, 7.25 × (10^(-2)) means 7.25 × 0.01 = 0.0725

Antiderivatives

Mathematica is pretty good at finding antiderivatives.

Integrate[x^3,x]

Mathematica says that (1/4)(x^4) is an antiderivative of (x^3). (Of course, there are others: (1/4)(x^4)+3, (1/4)(x^4)-18, etc.)

You can verify the correctness of an antiderivative by taking its derivative.

D[x^4/4,x]

Since the derivative of (1/4)(x^4) is (x^3), it is correct that (1/4)(x^4) is an antiderivative of (x^3). Good.

Integrals

f[x_]= Abs[x]-(3/12)(x+1)^2

Here is how Mathematica calculates the integral of f(x) over the interval [-1,4].

Integrate[f[x],{x,-1,4}]

Fundamental Theorem of Calculus, Part I

The first part of the Fundamental Theorem states that for any continuous function f on [a,b], the “Net Area So Far” function F(x)=(∫_a^x)f(t)dt is differentiable on [a,b] and F'(x)=(d/dx)(∫_a^x)f(t)dt=f(x).

F[x_]=Integrate[f[t],{t,-1,x},Assumptions->x≥-1];

(a is equal to -1 )

We can verify this in a couple ways. 
One, we can redo the earlier calculation.

F[4]

Two: In class we saw that F[x] is an antiderivative of f(x), so F’[x]=f(x).

g[x_]=D[F[x],x]

So g(x) should equal f(x).  Try it for any numbers (-1 or greater).

g[1.234]
f[1.234]

Exercises

Exercise 1: Using the demo provided

A Riemann Sum approximates the value of the integral.

What is the error when:
(1) n=4, using the initial function and left endpoints as the sample points?
(2) n=4, using the initial function and random sample points?
(3) n=4, using the function Sin(x) and random sample points?

Find an “n” which makes the error less than 0.05, when:
(1) using the initial function and left endpoints as the sample points.
(2) using the initial function and right endpoints as the sample points.
(3) using the initial function and random sample points.
(4) using the function Sin(x) and left endpoints as the sample points.
(5) using the function Sin(x) and right endpoints as the sample points.
(6) using the function Sin(x) and random sample points.

Although the answers are different, here is the important thing to notice: 
  You can always make the error as small as desired, simply by making “n” big enough.  
  (It works for every type of Rieman Sum, and it works for every function.)

Exercise 2

You choose a function and an interval.  Use Mathematica to:
(1) Find an antiderivative.
(2) Calculate the integral over the interval.