0

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$ 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*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[{n,f[n]}, {n,4,8,16,32}] somehow but I don't know how to include the interval [-1,1].

Kuba
  • 136,707
  • 13
  • 279
  • 740
ppkjref
  • 51
  • 1
  • 5

2 Answers2

2

There is an article called "Integral Approximations" at the Wolfram website:

http://library.wolfram.com/infocenter/Demos/4475/

with description: A palette for students to experiment with approximations to integrals by different methods. The palette allows the student to enter a function and range and sample rate and choose between rectanglular or trapezoid slices. The palette will draw the curve, the samples and show the actual and approximated value of the integral.

There is a notebook with code.

bill s
  • 68,936
  • 4
  • 101
  • 191
1

A straightforward, non-"functional" way to do it:

Table[method[f[x], {x, -1, 1}, n],
   {n, {4, 8, 16, 32}}, 
   {method, {midptApprox, trapex, simpson}}] // TableForm
murray
  • 11,888
  • 2
  • 26
  • 50