For one of my lab project I need to integrate a given function (in this example, f1
, using Simpson's ruls. When only one variable x
exists, I was able to find a code that calculate the integration:
f1[x_] := 3 + Cos[x + 3]
LEFT1[f_, a_, b_, n_] :=
Module[{h, i},
h = (b - a)/n;
N[h Sum[f1[a + i h], {i, 1, n - 1, 2}]]]
RIGHT1[f_, a_, b_, n_] :=
Module[{h, i},
h = (b - a)/n;
N[h Sum[f[a + i h], {i, 2, n - 2, 2}]]]
TRAP1[f_, a_, b_, n_] := 4 LEFT1[f, a, b, n] + 2 RIGHT1[f, a, b, n]
MID1[f_, a_, b_, n_] :=
Module[{h, i},
h = (b - a)/n;
N[h (f[a] + f[b])]]
SIMP1[f_, a_, b_, n_] := (MID1[f, a, b, n] + TRAP1[f, a, b, n])/3.0
SIMP1[f1, -1, 10, 10]
32.5062
Instead of using f1[x_] := 3 + Cos[x + 3]
, I would like to use f1[x_] = 3 + Cos[x + 3 b]
and calculate the integration for value of b
ranging from 1 to 50. I am not sure how I should start tackling this problem.