The integral is Integrate[Sqrt[1+x^3], {x, -1, 1}]
. I want to create a Table
with three columns and four rows that shows the midpoint, trapezoidal, and Simpson's rule approximations of the integral with n=4,8,16 and 32
$n=4,8,16$ and $32$ subintervals. I defined the three approximations as:
f[x] := Sqrt[1 + x^3]
midptApprox[func_, {x_, a_, b_}, n_] := Module[{f, h = (b - a)/n}, f[t_] := func /. x -> t; h \!\( \*UnderoverscriptBox[\(\[Sum]\), \(i = .5\), \(n - .5\)]\(f[ a + i\ h]\)\)]
trapex[func_, {x_, a_, b_}, n_] := Module[{f, h = N[(b - a)/n]}, f[t_] := func /. x -> t; 1/2 h (f[a] + 2 \!\( \*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n - 1\)]\(f[ a + i\ h]\)\) + f[b])]
simpson[func_, {x_, a_, b_}, n_] := Module[{f, h = (b - a)/n, wt}, wt[i_] := 3. + (-1)^(i - 1); f[t_] := func /. x -> t; If[EvenQ[n], h/3 (f[a] + \!\( \*UnderoverscriptBox[\(\[Sum]\), \(i = 1\), \(n - 1\)]\(wt[i] f[ a + i\ h]\)\) + f[b]), "n must be even"]]
f[x] := Sqrt[1 + x^3]
midptApprox[func_, {x_, a_, b_}, n_] := Module[{f, h = (b - a)/n},
f[t_] := func /. x -> t; h*Sum[f[a + i*h], {i, 0.5, n - 0.5}]]
trapex[func_, {x_, a_, b_}, n_] := Module[{f, h = N[(b - a)/n]},
f[t_] := func /. x -> t; (1/2)*h*
(f[a] + 2*Sum[f[a + i*h], {i, 1, n - 1}] + f[b])]
simpson[func_, {x_, a_, b_}, n_] := Module[{f, h = (b - a)/n, wt},
wt[i_] := 3. + (-1)^(i - 1); f[t_] := func /. x -> t;
If[EvenQ[n], (h/3)*(f[a] + Sum[wt[i]*f[a + i*h],
{i, 1, n - 1}] + f[b]), "n must be even"]]
I was thinking to use Grid
and TableForm@Table[nTableForm@Table[{n,f[n]}, {n,4,8,16,32}]
somehow but I don't know how to include the interval [-1,1]
.