testing collinearity with algorithm collinear7

13 次查看(过去 30 天)
Hi,
I was looking for an algorithm to test collinearity and found : http://blogs.mathworks.com/loren/2008/06/06/collinearity/
function tf = collinear7(p1,p2,p3)
mat = [p1(1)-p3(1) p1(2)-p3(2); ...
p2(1)-p3(1) p2(2)-p3(2)];
tf = det(mat) == 0;
works fine if you test it with eg: p1 = [1 1]; p2 = [3.5 3.5]; p3 = [-7.2 -7.2]; collinear(p1,p2,p3) ans=1
for testing I choose: p1 = [ 123.83 -205.59] p2 = [ 138.38 -990.38] p3=(p1+p2)/2 p3 = 131.11 -597.98
collinear(p1,p2,p3) ans = 0 here I was suprised because two points and the middle point should be collinear no? so I tried it again with p1=[1.5 -5.3] p2=[ 5 -3.8] p3=(p1+p2)/2 collinear(p1,p2,p3) ans = 1 so now it is ok again???
so I tested another approach slightky changed the code from the site
function tf = collinear1(p1,p2,p3)
m = slope(p1,p2);
b = intercept(p1,p2);
y = (m*p3(1)+b) ;
tf=abs(y- p3(2))<0.000001;
end
function m = slope(p1,p2)
m = (p2(2)-p1(2))/(p2(1)-p1(1));
end
function b = intercept(p1,p2)
m = slope(p1,p2);
b = p1(2)-m*p1(1);
end
with p1 = [ 123.83 -205.59] p2 = [ 138.38 -990.38] p3=(p1+p2)/2
collinear1(p1,p2,p3) is 1=> so collinear?
I can only think of numerical errors to explain this or am I doing something completely wrong?
regards,J

采纳的回答

Matt J
Matt J 2012-12-8
编辑:Matt J 2012-12-12
Yes, it's a numerical problem. The first version collinear7 uses the criterion
det(mat) == 0
which demands both perfect collinearity among the points and a perfect determinant calculation, with no tolerance for floating point errors. The other versions apply a tolerance on floating point inaccuracies.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by