What are the differences between integral and trapz?

159 views (last 30 days)
I don’t understand what are the differences between integral and trapz? How trapz works still don’t understand.

Answers (3)

the cyclist
the cyclist on 23 Jan 2023
Both functions perform numerical integration, but they use different methods. This is mentioned within the first couple of the documentation for integral and trapz. integral uses global adaptive quadrature, and trapz uses the trapezoidal method.

Walter Roberson
Walter Roberson on 23 Jan 2023
integral() accepts a function handle and is adaptive. Given two endpoints, it examines how steep the function is locally and choses extra points at places that seem to not be smooth. integral() can handle the case where the function is infinite at the endpoints.
trapz() is the trapazoid rule. It requires that the function having been evaluated already at a known set of locations. It then approximates the function as a series of adjacent trapazoids and calculates the total area of the trapazoids. It has no idea what the function is and there is no way for it to add extra points for locations that look rough. It cannot handle infinite or nan values.
In the special case that the distance between adjacent points is 1, then trapz() of a vector works out mathematically to be sum() of the vector minus (first point + last point)/2 .
  1 Comment
John D'Errico
John D'Errico on 23 Jan 2023
+1, though I want to disagree with one subtle inaccuracy. integral does not care about the steepness of the curve, so much as the curvature. So a straight line that is extremely steep is as easy to integrate as a line of low slope.
A good example of where we would see the problem is if we try to integrate the function
myfun = @(x) abs(x - pi);
over an interval like [3,4]. integral will need to put additional points near pi, because it will need to indentify where the break happens in the slope. At no point is the slope high, but the curvature of the function can be considered to be infinitely high.
And of course, if you specify a waypoint at pi, that would make all the problems go away.

Sign in to comment.


Steven Lord
Steven Lord on 23 Jan 2023
The integral function accepts a function handle as input and computes the integral of that function over an interval by evaluating the function at points of its choosing using adaptive quadrature. You cannot call it with a vector of data as it needs control over where the function is evaluated. The integral function may need to evaluate your function at many points, which can take time.
The trapz function accepts a vector of data as input and uses the trapezoidal rule to compute the integral of that data. You cannot directly call it with a function handle, you would need to evaluate the function handle yourself to sample your function and pass the results into trapz. If the points at which you sample the function do not well represent the behavior of the function you may receive an inaccurate answer.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!