The error of using command "integral"

I am trying to integrate a function , but the result is wrong. The strange thing is, if I integrate from -5 to 5, it is correct (0.0117). If I integrate it from -15 to 5, it will wrong (get 0). And then, if I integrate it from -15 to -5, the result is still 0.... I don't know where does matlab steal my 0.0117.............
The test code:
theta=4;
SP=@(X,D) univariate2dirboxspline_fast(cos(theta/180*pi),sin(theta/180*pi),X,D);
P2=0.394147314513724;
P3=1.391711364773548;
integral(@(X)SP(X,P2).*SP(X,P3),-5,5) % from -5 to 5 is 0.0117
integral(@(X)SP(X,P2).*SP(X,P3),-15,5) % from -15 to 5 is 0
integral(@(X)SP(X,P2).*SP(X,P3),-15,-5) % from -15 to -5 is 0
Related function, it can create a 2 dimension box spline, where D is the shifting value
function v = univariate2dirboxspline_fast(xi1, xi2, x,d)
%% Expects xi1, xi2, and x to be row vectors
xi = [xi1; xi2];
xi(abs(xi)<sin(1/180*pi))=0;
% x = abs(x - sum(xi)/2); % Symmetrize around 0
x=x-d;
x = abs(x);
l1 = vecnorm(xi, 1);
v = (l1/2-x) ./ abs(prod(xi));
h = 1 ./ vecnorm(xi, Inf);
v = max(v, 0);
v = min(v, h);
%v = piecewise(v > 0, v, 0);
%v = piecewise(v > h, h, v);
end

2 件のコメント

Walter Roberson
Walter Roberson 2019 年 1 月 1 日
You need to add waypoints .
Ziyu Shu
Ziyu Shu 2019 年 1 月 1 日
excuse me, what is waypoints?

サインインしてコメントする。

 採用された回答

Cris LaPierre
Cris LaPierre 2019 年 1 月 1 日
編集済み: Cris LaPierre 2019 年 1 月 1 日

0 投票

I can't answer why it's doing that, but when I plot your function, I can understand why it may be missing the peak. The peak you are trying to integrate is basically an impulse.
Integral likely automatically sets the integration step size (at least there is no way to set it), and it appears to be stepping right over the peak, missing it entirely in the integration. That may not be exactly true, but it makes sense to me.
To have it return the correct point, use the 'waypoints' name, value pair to force the integration to include specific points in the integration. I used the peak of the impulse, and now integral returns the correct value.
integral(@(X)SP(X,P2).*SP(X,P3),-5,5,'Waypoints',0.89)
ans = 0.0117
integral(@(X)SP(X,P2).*SP(X,P3),-15,5,'Waypoints',0.89)
ans = 0.0117
integral(@(X)SP(X,P2).*SP(X,P3),-15,-5,'Waypoints',0.89)
ans = 0
I understand that you will not likely always know what the peak point is. Perhaps you could differentiate the function and solve for the zero point? Anyway, hopefully this helps you get started.

1 件のコメント

Ziyu Shu
Ziyu Shu 2019 年 1 月 1 日
Thank you so much!
It turns out if the integrate distance is smaller than 10, such as -5,5 -6,4, the result is correct...

サインインしてコメントする。

その他の回答 (0 件)

タグ

質問済み:

2019 年 1 月 1 日

編集済み:

2019 年 1 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by