# Looking for good example of where numerical integration beats symbolic integration

11 views (last 30 days)
voxynn on 3 Oct 2017
Edited: John D'Errico on 3 Oct 2017
Hi,
I'm looking for a good example of where numerical integration beats symbolic integration to show a group of students why they should match their method to the problem. Aside from 'real world' lab data which can't be approximated by an equation, are there equations for which Matlab can't produce a nice integral solution using the Symbolic Math Toolbox, but can be well approximated using cumtrapz?

Mischa Kim on 3 Oct 2017
voxynn,
how about a RLC circuit? You could start with the linear DE, show that the DE can be solved symbolically and numerically, and then add a small non-linearity to the DE. Let me know if you need code and I can point you to it.

voxynn on 3 Oct 2017
This sounds interesting, as I have recently discussed RLC circuits in Matlab in terms of complex numbers (gain and phase shift) - a pointer to the code would be useful!
Mischa Kim on 3 Oct 2017
The code is part of the RLC circuit demo bundle on File Exchange. Check out RLC_symbolic.m and RLC_nonlinear.m .

John D'Errico on 3 Oct 2017
Edited: John D'Errico on 3 Oct 2017
There are multiple issues to consider. A symbolic integral is useful, even necessary, when you need a symbolic result. Trapz won't cut it there. If you have symbolic parameters in there, trapz is not an option at all, nor is an adaptive tool like any of the quad variants or integral.
From the other direction, is it easy to write a fully numeric problem that has no symbolic solution. Then a numerical rule is your only viable choice. Would I use trapz or cumtrapz? Probably not. These are very low order methods. Yes, there are cases where Euler's method is an acceptable choice to solve an ODE. But most of the time it is highly inefficient, and you are far better off using a better numerical method. The same logic applies to trapz.
So I tend to reserve trapz for numerical integration where I have only a data series. There it can be a good choice. In fact, one case where trapz is by far the preferred choice is when your data series has noise in it. A good thing for student is to investigate (if they have sufficient understanding of statistics) is to compare the variance of an estimate of an integral where the data is contaminated by numerical noise. The idea is that you have a signal that looks like...
y(i) + e(i)
Assume e(i) is gaussian noise with mean 0 & variance sigma^2, since integration is a linear operator, the integral becomes:
int(y) + int(e)
Now suppose you use a moderately high order Newton-Cotes rule, compared to trapz? Remember that the coefficients of a Newton-Cotes rule are not constant. For trapz, they are proportional to [1 2 2 2 2 ... 2 2 1]. But even for Simpson's rule, they look like [1 4 2 4 2 4 ... 4 2 1]. Higher order rules have a great deal more range in the coefficients. Now, compute that weighted variance of a Gaussian series when it is integrated using a few different Newton-Cotes rules. You will see that for high order rules, the variance of the noise integral starts to increase. So even if they integrate the function itself more accurately, the noise term can start to dominate.
The point is, trapz is a preferred tool for noisy series. For example, if you are integrating an acceleration series to get velocity then again to get displacement, use cumtrapz, not a higher order rule if those are measured accelerations. They will have noise in them.
Of course, if I did have a noisy series, I might prefer to pre-filter the data. So I might use a cubic smoothing spline to smooth the data, then integrate the spline.
It is always a good idea to have many different tools in your toolbox, but you need to know which one to pull out. I have a dozen or more different screwdrivers hanging on the wall in my shop. They are all there for a purpose, and I use them each when the need arises. I even have hammers hanging on that same wall. Would I use them to drive a screw? It might work, but not very well. Understanding each of your tools is crucial.