You are on page 1of 1

In[ ]:= ClearAll;

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

Exact int = 0.785398

Numerical error = 0.00206483

In[ ]:= ClearAll;


f[x_] = Exp[- x ^ 2];
a = 0;
b = 1;
h = (b - a) / 2;
apprint = (h / 3) (f[a] + 4 f[(a + b) / 2] + f[b]);
exctint = Integrate[f[x], {x, a, b}];
numerror = Abs[exctint - apprint];
Print["Approximate int using Simpson 1/3 = ", N[apprint]];
Print["Exact int = ", N[exctint]];
Print["Numerical error = ", N[numerror]];
Approximate int using Simpson 1/3 = 0.74718

Exact int = 0.746824

Numerical error = 0.000356296

You might also like