I have discrete data of a function plotted below: 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 as
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 could have more data points for the function, however, using 100 points or 1 million points does not change the result.
My data for the function and the reference solution are from a physical problem calculated through multiple long computational steps(very long to explain). Since my code and the the computations have already been verified for a *better function, and since the same problem was reported in a paper (that singularity begins for this setting and the numerical integration becomes harder), I think the problem is related to the numerical integration, probably because of the roundoff errors and the 16digits precision of Matlab.
I also did all of the computations from the first of the simulation using VPA. for each line of code, i used VPA atleast once (In Matlab website, If i understand correctly, it is written that if one of the numbers/variables are VPA, whole computation in that line is done using higher precision). I mean, my data (y values) were calculated using VPA before being given to TRAPZ function. Then, I used
trapz(x,vpa(y))% y also has already been calculated using VPA.
About X values, i could not make it higher precision I am working on that.
With and without VPA: Using 10 points and Trapz, I get : 0.299517049200365 , (I get nearly the same with "Integral" function as i explained above.) Using 100k points and Trapz, I get: 0.299617122167918, The reference solution is : 0.220712757091533.
I wrote the same question in another website, and people used my data (*for 100k) points and also got the same result (0.29...). They explained me that such 30% error is not happening because of the integration and it is related to my data and reference solution. Is there any chance that we missed some point here?
in my physical problem, I can change some parameters, which leads to a better function (not near-zero values around x=1.57). The more my function becomes like this, the more my error increases.