Is the Trapz feature always accurate?

30 views (last 30 days)
Pouyan Msgn
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
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);
clear all
D1=flipud(J26); %It is a spectrum in reverse x-axis
X1=D1(:,1); Y1=D1(:,2);
X2=D2(:,1); Y2=D2(:,2);
X3=D3(:,1); Y3=D3(:,2);
set(gca, 'XDir','reverse')
I188 =
Is it right? I know these are numeric algorithms but can't it be more accurate?
  1 Comment
David Goodmanson
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).

Sign in to comment.

Answers (1)

John D'Errico
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.
ans =
spl = spline(X1,Y1/Y1(1));
ans =
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.


Community Treasure Hunt

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

Start Hunting!