Should you use Heaviside step functions to create an integral that is a piecewise discontinuous function, NO. NO. NO!!!!!! There, I said it three times, so it must be true.
Never intentionally create a function with discontinuities and then try to integrate it. Even if integral succeeds, the integration will be far more inefficient than it would be by simply splitting the problem up into segments. Use of a step function to combine pieces together is simply a lazy trick, hoping that integral will be smart enough to survive. Never be tricky, and hope that a computer algorithm will survive your trickery. Too often, it won't be.
Note that integral has the capability to provide waypoints. If you know there is a problem in some spot, then you need a waypoint.
Problems might be a discontinuity, or a singularity of some sort, or something that would cause the integration to fail. For example, the presence of a near delta function.
fun = @(x) exp(-50*(x-31.25).^2);
integral(fun,-1000,1000)
ans =
0
integral(fun,-1000,1000,'waypoints',[30 31.25 32])
ans =
0.25066
Yes, a waypoint will be a great improvement. For example, if we tried to integrate a simple step function.
step = @(x) double (x>pi);
timeit(@() integral(step,0,12))
ans =
0.0020337
timeit(@() integral(step,0,12,'waypoints',pi))
ans =
0.00052395
format long g
integral(step,0,12)
ans =
8.85840830988541
integral(step,0,12,'waypoints',pi)
ans =
8.85840734641021
12 - pi
ans =
8.85840734641021
As you can see, the form with a waypoint is both significantly faster, as well as wildly more accurate.
Best Answer