MATLAB: Error when trying to integrate over step function

integrationMATLABnumerical integrationstep function

I am trying to integrate a step function and I keep getting error messages.
Here is my code:
>> integral(step_fun,5,7)
Error using step_fun (line 4)
Not enough input arguments.
The step_fun is defined in a separate code file as:
function y = step_fun(x)
y = 0;
if x >= 0
y = 1;
end;
end
If I try:
func=@(t) step_fun(t)
integral(func,5,7)
I got:
Error using integralCalc/finalInputChecks (line 515)
Output of the function must be the same size as the input. If FUN is an
array-valued integrand, set the 'ArrayValued' option to true.
Error in integralCalc/iterateScalarValued (line 315)
finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
What does this mean? Thank you.

Best Answer

integral() passes in a vector of values and expects one result for each element of the vector. You need to vectorize:
function y = step_fun(t)
y = zeros(size(t));
y(t>=0) = 1;
Or more compactly,
function y = step_fun(t)
y = t >= 0;
which can be done as
step_fun = @(t) t >= 0;