Composite Trapezoid Rule: Difference between revisions

Content added Content deleted
No edit summary
Line 5: Line 5:


function integral = trapezoidal(f, a, b, n)
function integral = trapezoidal(f, a, b, n)
h = (b-a)/n;
x = (b-a)/n;
result = 0.5*f(a) + 0.5*f(b);
result = 0.5*f(a) + 0.5*f(b);
for i = 1:(n-1)
for i = 1:(n-1)
result = result + f(a + i*h);
result = result + f(a + i*x);
end
end
integral = h*result;
integral = x*result;
end
end