Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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].

share|improve this question

2 Answers 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.

share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.