Is the Trapz feature always accurate?
30 views (last 30 days)
I'm analyzing some spectrum ... In another program FitXPS I enter some data and want to integrate the intensity (which is in Y-axis)
Here is one spectrum:
In the FitXPS, the integral values are:
J26 = 468
Observe that in this area I have to normalize in order to get Y=1 at the lowest X value so Y=Y/Y(1);
D1=flipud(J26); %It is a spectrum in reverse x-axis
Is it right? I know these are numeric algorithms but can't it be more accurate?
John D'Errico on 23 Mar 2020
Edited: John D'Errico on 23 Mar 2020
trapz is not a "feature". It is a piece of software that embodies the trapezoidal rule, something that students may be exposed to perhaps in high school, or at least in college classes. Nothing special about it. Is that a highly accurate method? No. Can it be more accurate? Of course not. It is what it is - a very basic rule learned by students, one that is also useful for people at many levels.
(An important thing to understand about a basic rule like trapz is it is also more robust to integrating noisy data. And any data that is measured will often have some degree of noise in it too. As such trapz is often the preferred tool for many problems.)
Since we do not know the true area under those curves, we cannot be sure. All you have are sampled values.
Something we don't know is what is FitXPS is doing. Is that some sort of smoothing spline? As such, it is likely doing some serious smoothing, so it may well be greatly missing those sharp peaks.
Another important point is that you are normalizing these curves by dividing by Y(1), a location where the value is virtually tiny compared to the rest of your data. When you divide by a comparatively small number, expect accuracy to go to hell.
Anyway, how accurate is trapz here? Again, that is difficult to know, ince we don't know the true underlying function to integrate. All we have is a list of points.
spl = spline(X1,Y1/Y1(1));
I've compared it to an integral based on integrating an interpolating spline fit. your curve seems fairly smooth, so this should be pretty decent.
How close is the trapz estimate to that produced by a spline integral (which is effectively a higher order approximation to the integral)? They differ only in the 6th significant digit. If you got something that was wildly different, I would wonder what it is doing to your curve. My guess is that tool is DRAMATICALLY smoothing your curve. As such, the highly inaccurate estimate is the crap that FitXPS did to your data, not what trapz returns.
In fact, even if I try using a smoothing spline on that curve, I still get something quite close to what trapz gave. Your problem is not trapz, but whatever completely unknown thing that FitXPS is doing.