This lab pursues the question of integration from a numerical perspective. In addition to the rectangular and trapezoidal methods seen earlier, the Simpson method is introduced. Each of these methods have error bounds and we will explore these.
New Mathematica commands to (eventually) learn: Integrate, NIntegrate
Clear[f, x]
f[x_] := -2(x + 1)(x - 1)(x - 3)
Plot[f[x], {x, 1, 3}, Filling -> Axis]
LeftApprox[f[x], {x, 1, 3}, 8]
RightApprox[f[x], {x, 1, 3}, 8]
MidpointApprox[f[x], {x, 1, 3}, 8]
TrapezoidApprox[f[x], {x, 1, 3}, 8]
SimpsonApprox[f[x], {x, 1, 3}, 8]
Clear[h, x]
h[x_] := x^2
ShowApproximations[h[x], {x, 1, 3}, left, 8];
ShowApproximations[h[x], {x, 1, 3}, right, 8];
ShowLeftRightDifference[h[x], {x, 1, 3}, 8];
Q3. What is the area of these blue rectangles? Note that they each have the same width. Furthermore, observe that the height of the top of the ith rectangle is the height of the bottom of the (i + 1)st rectangle. Therefore, imagine sliding them all together on one stack (say, on the first rectangle). How great is this height? Consequently, what is the area?
ShowApproximations[h[x], {x, 1, 3}, trapezoid, 8];
ShowApproximations[Sin[x], {x, 1, 3}, trapezoid, 2];
ShowTrapError[Sin[x], {x, 1, 3}, 2];
ShowApproximations[1/2 x, {x, 1, 5}, left, 6, PlotRange -> {1/2,10}, AxesOrigin -> {1,0}];
ShowApproximations[2 x, {x, 1, 5}, left, 6, PlotRange -> {1/2,10}, AxesOrigin -> {1,0}];
Q6. Which function has more error? Why?
functlist = {1/10 x - 4, 1/4 x + 2, x, 5x - 3, -9x + 3, 15 x, -23 x + 1};
TableForm[Table[{functlist[[i]], est = NLeftApprox[functlist[[i]], {x,0,4}, 6], act = Integrate[functlist[[i]], {x, 0, 4}]//N, error = act - est,Abs[error]}, {i, 1, Length[functlist]}], TableHeadings -> {None, {"f", "L(6)", "I = exact", "I - L(6)", "|I - L(6)|"}}, TableAlignments -> {Automatic, Center}]
TableForm[Table[{2^i, est = NLeftApprox[x, {x, 0, 4}, 2^i], act = Integrate[x, {x, 0, 4}]//N, Abs[act - est]}, {i, 0, 6}], TableHeadings -> {None, {"n","L(n)", "I = exact", "|I - L(n)|"}}, TableAlignments -> {Automatic, Center}]
Clear[h, x, a, b, n]
h[x_] := x^2
a = -3; b = 3; n = 8;
ShowApproximations[h[x], {x, a, b}, trapezoid, n];
ShowApproximations[h[x], {x, a, b}, midpoint, n];
{StringJoin["M(30) = ",NMidpointApprox[h[x], {x, a, b}, 30]//ToString], StringJoin["T(30) = ",
NTrapezoidApprox[h[x], {x, a, b}, 30]//ToString]}
RectToTrap;
Clear[f, x, a, b, n]
f[x_] := 895 - 1404x + 1282x^2 - 976x^3 + 499x^4 - 139x^5 + 19x^6 - x^7
a = 1; b = 6; n = 10;
ShowApproximations[f[x], {x, a, b}, simpson, n];
TableForm[Table[{n,NLeftApprox[f[x],{x, a, b}, n],NRightApprox[f[x],{x, a, b}, n], NTrapezoidApprox[f[x],{x, a, b}, n],NMidpointApprox[f[x],{x, a, b}, n], NSimpsonApprox[f[x],{x, a, b}, n]}, {n, 8, 30, 4}], TableHeadings -> {None, {"n","L(n)","R(n)","T(n)","M(n)","S(n)"}}, TableAlignments ->{Automatic, Center}]
(Note that the midpoint column got cut off in part, as did the Simpson column in entirety.)
Return to the main page.
Go to the next lab.