If else if statement problem

35 views (last 30 days)
Lord Chinex on 15 Jul 2014
Commented: Roger Stafford on 15 Jul 2014
function I = compvec(x,y)
%Calculates the angle
c = ((dot(x,y))/((sqrt(x(1)^2 + x(2)^2)) * (sqrt(y(1)^2 + y(2)^2))))
if x==0 | y==0
disp('Linearly dependent vectors (one of them is zero).r')
disp('{x, y} is a linearly dependent set.')
%Problem occurs here, if c = 1, else statement is displayed instead
elseif c == 1 || c == -1
disp('Linearly dependent vectors (both non-zero).')
disp('{x, y} is a linearly dependent set.')
elseif c == 0
disp('Linearly independent vectors (orthogonal).')
disp('{x, y} is a linearly independent set.')
else
disp('{x, y} is a linearly independent set.')
end

David Sanchez on 15 Jul 2014
You can solve the issue rounding the value of c:
%Problem occurs here, if c = 1, else statement is displayed instead
elseif round(c) == 1 || round(c) == -1
disp('Linearly dependent vectors (both non-zero).')
disp('{x, y} is a linearly dependent set.')
elseif round(c) == 0
disp('Linearly independent vectors (orthogonal).')
disp('{x, y} is a linearly independent set.')
else
disp('{x, y} is a linearly independent set.')
end
Lord Chinex on 15 Jul 2014
what if I had a c = 0.5? Wouldn't it be rounded incorrectly to 1.

Julia on 15 Jul 2014
Edited: Julia on 15 Jul 2014
This could be due to rounding issues. It is very unlikely that you get the exact value 1 in Matlab.

Roger Stafford on 15 Jul 2014
There is more than one thing amiss in this piece of code. Probably the problem you have in mind is caused by your 'if' statement:
if x==0 | y==0
Both x and y are (presumably) two-element vectors, so the logical expression
x==0 | y==0
also has two logical elements. One of these is "x(1)==0|y(1)==0" and the other is "x(2)==0|y(2)==0". The 'if' will execute if both of these are true, and that is clearly not what you intended. For example the 'if' will execute in case x(1)==0 and y(2)==0, even though neither the x vector nor the y vector is a zero vector. What you should have written is:
if (x(1)==0 and x(2)==0) | (y(1)==0 and y(2)==0)
or more compactly
if all(x==0) | all(y==0)
You could even do:
if norm(x)==0 | norm(y) == 0
My next objection is requiring exact equality in
elseif c == 1 | c == -1
You could well have essentially parallel vectors but due to round-off errors in the computation of c, there would not be exact equality to 1 or -1. You need to allow a tolerance for a small deviation from exact equality here. The same applies to the test "elseif c == 0" for orthogonality.
One final complaint. You take the test for zero vector too late to avoid getting a NaN for an answer in computing c. You should have made this test beforehand to avoid a possible NaN.
(Note: As you undoubtedly realize, you are computing the cosine of the angle rather than the angle between those two vectors.)
Roger Stafford on 15 Jul 2014
Also I should point out that
abs(x(1)*y(2)-x(2)*y(1)) < tol
and
abs(dot(x,y)) < tol
where 'tol' is a very small tolerance for roundoff errors, are good tests for vectors x and y being parallel and orthogonal, respectively.