You are on page 1of 1

In[ ]:= ClearAll;

f[x_] = 1 / (1 + x ^ 2);
a = 0;
b = 1;
n = 4;
h = (b - a) / n;
apprint = (h / 2) (f[a] + 2 * Sum[f[a + i * h], {i, 1, n - 1}] + f[b]);
exctint = Integrate[f[x], {x, a, b}];
numerror = Abs[exctint - apprint];
Print["Approximate int using Composite trapezoid = ", N[apprint]];
Print["Exact int = ", N[exctint]];
Print["Numerical error = ", N[numerror]];

Approximate int using Composite trapezoid = 0.782794

Exact int = 0.785398

Numerical error = 0.00260405

In[ ]:= ClearAll;


f[x_] = Exp[- x ^ 2];
a = 0;
b = 1;
n = 20;
h = (b - a) / n;
apprint = (h / 2) (f[a] + 2 * Sum[f[a + i * h], {i, 1, n - 1}] + f[b]);
exctint = Integrate[f[x], {x, a, b}];
numerror = Abs[exctint - apprint];
Print["Approximate int using Composite trapezoid = ", N[apprint]];
Print["Exact int = ", N[exctint]];
Print["Numerical error = ", N[numerror]];
Approximate int using Composite trapezoid = 0.746671

Exact int = 0.746824

Numerical error = 0.000153296

You might also like