Integration and Differentiation in MATLAB: Video Tutorial
Compute integrals and derivatives in MATLAB using functions or sampled data. Explore quadrature, trapezoidal integration, gradients, tolerances and sample spacing.

On this page
This video tutorial reviews and implements integration and differentiation in MATLAB. It is part of the Numerical Computations course. To watch the full course, visit the course page.
Two kinds of problems
The four videos cover two situations:
- You have a function. The integral is computed by adaptive quadrature: the solver evaluates at chosen points and refines the intervals until the error is small enough. MATLAB offers
integralandquadgkfor one variable,integral2andquad2dfor double integrals, andintegral3for triple integrals. - You have samples. When only values at points are known, sums replace integrals and differences replace derivatives. MATLAB offers
sum,cumsum,trapzandcumtrapzfor integration, anddiff,gradientanddel2for differentiation.
Watch online
The four sections of this tutorial are available on YouTube and are embedded here as a playlist.
What each video covers
Section 1: Numerical integration using integral and quadgk
Watch this part on YouTube (opens in a new tab).
The basic call is integral(f, a, b), where f is a function handle. The video starts with , then integrates from 0 to 1 (about ) and from 0 to infinity (exactly ). The integrand uses element-wise operators (.*, ./, .^), because integral evaluates it on many points at once.
f = @(x) x.*exp(-x.^2);
q = integral(f, 0, Inf)Next comes , which has a singularity at the lower limit. The video uses this example to explain the error tolerances. If is the exact value and the computed one, the solver tries to satisfy
Setting 'RelTol' to 0 leaves only the absolute tolerance in control. A smaller 'AbsTol' requests a tighter error estimate, but it does not remove floating point limits or guarantee that every difficult integrand reaches that accuracy. Check any warning from the solver.
The video then computes contour integrals in the complex plane. The integral of around the unit circle is (Cauchy’s theorem says it would be zero if the path enclosed no pole). It is first computed with the parametrization :
A simpler way uses the 'Waypoints' option: the path starts and ends at 1 and passes through , and . A triangular path through and does not enclose the pole at the origin, and the result is almost zero.
Finally, quadgk (adaptive Gauss-Kronrod quadrature) is called in the same way and gives the same results. It accepts the same tolerance and waypoint options, plus 'MaxIntervalCount'.
Section 2: Double and triple integrals
Watch this part on YouTube (opens in a new tab).
integral2(f, xmin, xmax, ymin, ymax) and quad2d compute double integrals, and integral3 computes triple integrals. The limits of are numbers, while the limits of (and ) can be numbers or function handles of the outer variables. The video solves three examples:
In the second integral, the upper limit of depends on , so it is passed as a function handle:
g = @(x, y) 1./(sqrt(x + y).*(1 + x + y).^2);
ymax = @(x) 1 - x;
q = integral2(g, 0, 1, 0, ymax)Section 3: Discrete integrals and summations
Watch this part on YouTube (opens in a new tab).
In discrete systems, a sum plays the role of an integral. The video shows sum and cumsum on vectors and on a matrix along either dimension, and sum(Y(:)) for the total of all elements.
It then approximates with 100 samples. The plain sum of the samples times is close to 1. trapz uses the trapezoidal rule and gives a better result. With equal spacing you can pass the spacing or multiply by it. With unequal spacing you must pass the vector of values. cumtrapz returns the running integral, which the video plots against . Nested calls of trapz give double and triple integrals.
x = linspace(0, pi/2, 100);
y = sin(x);
q = trapz(x, y)Section 4: Numerical differentiation
Watch this part on YouTube (opens in a new tab).
diff is the opposite of cumsum: for a vector it returns the differences , so the result has one element less. The derivative of on 100 points from 0 to is then approximated by the forward difference
and compared with the exact derivative, . Reducing the spacing improves the approximation in this smooth example until rounding error becomes important. Differencing noisy measurements can amplify the noise.
gradient returns a result of the same length as the input and gives a smaller error than diff in this example. It also works in several dimensions. For on a grid with spacing , [gx, gy] = gradient(Z, h) returns both partial derivatives, and quiver draws the gradient vectors over a contour plot.
The last function is del2, which is related to the Laplacian
del2 returns , where is the number of dimensions. For a function of two variables, the video multiplies the result by 4 to get the Laplacian.
Applications
- Areas, volumes and other quantities defined by integrals that have no closed form.
- Integrating measured data, such as velocity samples, to get a total or a running total.
- Estimating rates of change and gradients from sampled or grid data.
Check units and spacing
Pass the actual sample coordinates when measurements are not evenly spaced. A raw sum or difference does not include the sample spacing, so it does not by itself have the units of an integral or derivative. For a new integrand, compare tolerances and check against an analytic result when one is available. For sampled data, repeat the calculation on a finer grid when possible and check whether the result stabilizes.
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Integration and Differentiation in MATLAB: Video Tutorial (YPTNM190520S12), Yarpiz, 2019. https://yarpiz.com/integration-and-differentiation-video-tutorial-yptnm190520s12@misc{yarpiz-yptnm190520s12,
author = {{Mostapha Kalami Heris}},
title = {{Integration and Differentiation in MATLAB: Video Tutorial}},
howpublished = {Yarpiz},
year = {2019},
note = {Code YPTNM190520S12},
url = {https://yarpiz.com/integration-and-differentiation-video-tutorial-yptnm190520s12}
}Published Updated

Comments