Function Repository Resource:

IntegralApproximationPlot

Source Notebook

Compute and plot the approximation to the integral of a function on an interval

Contributed by: Dennis M Schneider

ResourceFunction["IntegralApproximationPlot"][f,{x,xmin,xmax},method,partition]

plots f and an approximation to the integral of f over the interval xminxxmax based on the approximation method and the type of the partition.

Details and Options

The possible values of method are listed below and follow standard mathematical usage, except for possibly "Riemann". When method is set to "Riemann", a random point in each subinterval is selected for the height of the approximating rectangle.
"Left"the height of the approximating rectangle is determined by the value of the function at the left-hand endpoint of each subinterval
"Midpoint"the height of the approximating rectangle is determined by the value of the function at the midpoint of each subinterval
"Right"the height of the approximating rectangle is determined by the value of the function at the right-hand endpoint of each subinterval
"Upper"the height of the approximating rectangle is determined by the maximum value of the function on each subinterval
"Lower"the height of the approximating rectangle is determined by the minimum value of the function on each subinterval
"Trap"the approximation is determined by a trapezoid determined by the heights at pairs of endpoints
"Simpson"the approximation is determined by quadratic polynomials determined by the endpoints and midpoint of the intervals
"Riemann"the height of the approximating rectangle is determined by the value of the function at a random point in each subinterval
Possible partitions are {"Regular",n} for a regular partition with n equal subintervals, {"Maxdx",dx} for a partition with mesh less than dx or an ordered list {x1,x2,,xn} specifying the partition points.
"Exact" is an option for ResourceFunction["IntegralApproximationPlot"] that, if set to True, will return the exact value of the approximation when the partition points are not approximate real numbers.
"DrawGraph" is an option for ResourceFunction["IntegralApproximationPlot"] that, if set to False, will suppress the graph and just return the approximation to the area of the region.
"PolyStyle" is an option for ResourceFunction["IntegralApproximationPlot"] that applies styles to quadratic polynomials used in Simpson’s approximations.
"PrintDisplay" is an option for ResourceFunction["IntegralApproximationPlot"] that, if set to False, will suppress printing of the approximation or the maximum mesh size.

Examples

Basic Examples (10) 

A left approximation using a regular subdivision with 20 subintervals:

In[1]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, \[Pi]}, "Left", {"Regular", 20}]
Out[1]=

An upper approximation using a regular subdivision with 15 subintervals:

In[2]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, \[Pi]}, "Upper", {"Regular", 15}]
Out[2]=

Suppress the plot and just return the approximation:

In[3]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, \[Pi]}, "Upper", {"Regular", 15}, "DrawGraph" -> False]
Out[3]=

Specify the mesh size of the partition:

In[4]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, \[Pi]}, "Midpoint", {"Maxdx", .2}]
Out[4]=

The option "Exact" does not apply to partitions containing approximate real numbers:

In[5]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, \[Pi]}, "Midpoint", {"Maxdx", .2}, "Exact" -> True]

Return the exact approximation for a partition using exact numbers:

In[6]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, \[Pi]}, "Midpoint", {"Regular", 9}, "Exact" -> True]
Out[6]=

Apply a style to the polynomials used in Simpson’s approximation:

In[7]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, 4}, "Simpson", {"Regular", 12}, "PolyStyle" -> {{Orange, Thickness[.02]}}]
Out[7]=

Add a style to the filling:

In[8]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, 4}, "Simpson", {"Regular", 12}, "PolyStyle" -> {{Orange, Thickness[.02]}}, FillingStyle -> LightRed]
Out[8]=

Use a user-defined partition:

In[9]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, 4}, "Left", {0, 1/4, 3/4, 5/4, 9/4, 3, 15/4, 4}, "Exact" -> True]
Out[9]=
In[10]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, 4}, "Trap", {0, 1/4, 3/4, 5/4, 9/4, 3, 15/4, 4}, "Exact" -> True]
Out[10]=

Use Manipulate to illustrate all possible combinations of methods and partitions (except user-defined partitions):

In[11]:=
Manipulate[
 ResourceFunction["IntegralApproximationPlot"][Sin[x^2], {x, 0, 4}, method, If[
   StringMatchQ[ToString[partition], "Regular"], {"Regular", n}, {"Maxdx", dx}], "PrintDisplay" -> False, ImageSize -> 400, Frame -> True], {method, {"Left", "Midpoint", "Right", "Upper", "Lower", "Trap", "Simpson", "Riemann"}}, {partition, {"Regular", "Maxdx"}}, {n, Range[10, 60, 10]}, {dx, Range[.5, .05, -.05]}, SaveDefinitions -> True]
Out[11]=

Possible Issues (1) 

Simpson’s method will not accept a user-defined partition:

In[12]:=
ResourceFunction["IntegralApproximationPlot"][
 Sin[x^2], {x, 0, 4}, "Simpson", {0, 1/4, 3/4, 5/4, 9/4, 3, 15/4, 4}]

Publisher

Dennis M Schneider

Version History

  • 2.0.0 – 28 August 2019
  • 1.0.0 – 02 August 2019

Related Resources

License Information