Numerical integration of discrete data using higher precision
Show older comments
I have discrete data of a function. I attach the data for cases with 10 points and 100k points and also the plotted function.
The "Y" values of the function near "X=1.57" are very close to each other and zero, like 9.25558265263186E-11 and 5.92357284527227E-11.
Using Trapz function directly, or using Integral function indirectly something like
f1 = griddedInterpolant(x,y); %I used also spline and other methods
f2 = @(t) f1(t);
Result = Integral(f2,0,pi/2)
fails with more than 30% relative error.
I assume it happens because of the roundoff problem and the 16digits precision of matlab.
Is there any way to force Trapz to do the integration using higher precision?
I tried VPA for the given input data to Trapz. But didnt work. Appreciate any suggestion.
4 Comments
Jan
on 29 Dec 2021
Please post the code you used for VPA and trapz and explain "But didnt work" with details.
Msimon q
on 29 Dec 2021
Higher precision does not really help, IF the numbers themselves are not known correctly and exactly.
For example, what is the value of a simple computation:
X = sqrt(sym(2))
Y = sym(17)^(10*pi) + X
In symbolic form, they have nice algebraic values. Hopwever, if we compute those numbers as floating point numbers, within a double precision form?
format long g
dx = double(X)
Effectively, we only know that number now to an accuracy of
eps(dx)
That is the value of the least significant bit. As stored as a double precision number, that number is stored in BINARY form, as
sprintf('%0.55f',dx)
But the true value of sqrt(2) is
vpa(X,55)
As you can see, the two numbers begin to diverge after around the 16 decimal place. So once you create a number as a double precision value, you have no information content beyond the 16th decimal digit. And that means all of those numbers you created, that you think are known exactly, are not known as exactly as you think.
Now, how about Y? Actually, Y is a number with many digits to the left of the decimal point.
vpa(Y,55)
eps(double(Y))
So the least significant bit in the value of Y, IF we form a double precision mnumber from it, is a big number in itself. Anything below that value is virtually meaningless numerical garbage. So can we form the value X+Y, as a double, and have the lower significant digits mean anything? Certainly not.
dx < eps(double(Y))
My point in all of this, is using vpa here is a ruse. It convinces you that you were being "safe" In fact, those numbers, since they were originally stored as doubles, have no information content below a certain point. You could have used a million digits, and not have been safe.
Msimon q
on 30 Dec 2021
Accepted Answer
More Answers (1)
Jan
on 29 Dec 2021
You explain, that you have discrete data, but the code you show uses a function. If the data are given in double precision, using a higher precision for the intration has a very limited use only: The result cannot be more accurate than the input.
But the sum is numerically instable. So you can try a summation with error compensation: FEX: XSum
Take a look into the code of trapz. You find this (for equidistant x values):
x * sum((y(1:end-1,:) + y(2:end,:)), 1)/2
Replace this by:
x * XSum((y(1:end-1,:) + y(2:end,:)), 1)/2
4 Comments
Torsten
on 29 Dec 2021
I think error compensation can only occur if the function has regions of y-data of different sign.
Msimon q
on 29 Dec 2021
Jan
on 30 Dec 2021
Fine. This is another evidence for the fact, that the accuracy of the integral is not the problem, but that the reference value is incorrect.
Categories
Find more on Numerical Integration and Differentiation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!