MATLAB: Trapezium Rule example won’t work

functiontrapezium rule

I do not understand why this code is not working? I am trying to estimate the integral of e^-x2 between x=0 and x=2 using the trapezium rule

Best Answer

You're combining a function declaration and a function call.
Call your function something other than trapz because that's already a function in MATLAB. Let's call it IssieTrapz. It takes four input arguments, which I suspect your function accepts in the following order:
function I = IssieTrapz(f, a, b, n)
This is line 1 of your function. It replaces your original line 1. That's the declaration of the IssieTrapz function.
Now when you call IssieTrapz, that's when you specify the function and give values for a, b, and n.
result = IssieTrapz(@(x) exp(-x^2), 0, 2, 10);