I have tried to add a new method in the Ms, basically I want to follow this formula:
T - \frac{(f'(b) - f'(a))h^{2}}{12}
with T is the result from the Trapezoidal, and h = \frac{b-a}{n}, n is the number of partition, and the interval is [a,b]
it seems the code still not produces the correct calculation, I shall try again:
...
modifiedtrapezoid = (f,a,b) -> (f(a) + f(b))/2 - (((f'(b)-f'(a))*h^2)/12),
...
Edited, this code works:
using CalculusWithJulia
# For partition of 8
ns = (9)
# Define the function, the partitions, and the interval
f(x) = x^4
a = 1
b = 3
n = 8
h = (b-a)/n
g = (((f'(b)-f'(a))*h^2)/12)
function riemann(M, f, a, b, n)
xs = range(a,b,n)
riemann(M, f, xs[1:end-1], xs[2:end])
end
riemann(M, f, as, bs) = sum(M(f,a,b) * (b-a) for (a,b) in zip(as, bs))
Ms = (left = (f,a,b) -> f(a),
right= (f,a,b) -> f(b),
midpoint = (f,a,b) -> f(a/2 + b/2),
#mΜ = (f,a,b) -> first(findmin(f, range(a,b,10))),
#MΜ = (f,a,b) -> first(findmax(f, range(a,b,10))),
trapezoid = (f,a,b) -> (f(a) + f(b))/2,
modifiedtrapezoid = (f,a,b) -> (f(a) + f(b))/2 - g/2,
simpsons = (f,a,b) -> (c = a/2 + b/2;(1/6) * (f(a) + 4*f(c) + f(b)))
)
using DataFrames
d = DataFrame(n = Int[], M = Symbol[], value = Float64[])
for (m,M) in pairs(Ms)
for n in ns
v = riemann(M, f, a, b, n)
push!(d, (n=n-1, M=m, value=v))
end
end
unstack(d, :n, :value)