I created this code to calculate an approximation to an integral, using the Trapezoid Rule and the Midpoint Rule.
Here the output is a table for N=16.
Question: what would be a simple way to create a table multiple values for N? Such as N=4, N=8 and N=16?
Clear["Global`*"]
n := 16
xA := 0
xB := 2
f[t_] := 1 + t^2
int := Integrate[f[t], {t, xA, xB}] // N
h := (xB - xA)/n
xT := xA + h*Range[0, n]
yT := f[xT]
T := h (Total[yT] - 1/2 First[yT] - 1/2 Last[yT]) // N
Terror := int - T
xM := xA + h*(Range[1, n] - 1/2)
yM := f[xM]
M := Total[h*f[xM]] // N
Merror := int - M
Grid[{{"Integral", "T", "T error", "M", "M error"}, {int, T, Terror,
M, Merror}}, Frame -> All]
I was hoping to simply declare a list: n={4,8,16}
, but apparently that doesn't work. This kind of surprises me, because something like this would work:
g[x_] := x^2
mylist = {4, 8, 16}
g[mylist]
n
. $\endgroup$List
as an argument the same as if the function were mapped onto theList
, the function must either 1) have the attributeListable
or 2) be composed of functions all of which areListable
. In your example,g
acts as if it had the attributeListable
because its only component (Power
) isListable
. $\endgroup$