# Is the Trapz feature always accurate?

30 views (last 30 days)
Pouyan Msgn on 23 Mar 2020
Edited: John D'Errico on 23 Mar 2020
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
J29=28.4
J188=306
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);
clc
clear all
J26=importdata('J26.txt');
J29=importdata('J29.txt');
J188=importdata('J188.txt');
D1=flipud(J26); %It is a spectrum in reverse x-axis
D2=flipud(J29);
D3=flipud(J188);
X1=D1(:,1); Y1=D1(:,2);
X2=D2(:,1); Y2=D2(:,2);
X3=D3(:,1); Y3=D3(:,2);
I26=trapz(X1,Y1/Y1(1))
I29=trapz(X2,Y2/Y2(1))
I188=trapz(X3,Y3/Y3(1))
plot(X1,Y1/Y1(1))
set(gca, 'XDir','reverse')
I26=
698.4176
I29=
44.5235
I188 =
404.2828
Is it right? I know these are numeric algorithms but can't it be more accurate?
David Goodmanson on 23 Mar 2020
Hello Pouyan,
The trapezoidal rule does a linear approximation between data points, so it is only going to be so "accurate". In this case I believe it is pretty good, certainly much, much better than the difference between 698 and 468 (for J26) would indicate. So, how does fitXPS come up with its results? Does it fit the peaks and not use the uninvolved, nonpeak intensity? And as for normalizing by the value of y(1) (if that were indeed what is going on), y(1) is just a single point, far from the action. It might make sense if you were normalizing with an average baseline intensity, but that's not y(1).

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.
trapz(X1,Y1/Y1(1))
ans =
698.417617848148
spl = spline(X1,Y1/Y1(1));
fnval(fnint(spl),X1(end))
ans =
698.418075726444
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.