필터 지우기
필터 지우기

If else if statement problem

조회 수: 6 (최근 30일)
Lord Chinex
Lord Chinex 2014년 7월 15일
댓글: Roger Stafford 2014년 7월 15일
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
David Sanchez 2014년 7월 15일
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
  댓글 수: 1
Lord Chinex
Lord Chinex 2014년 7월 15일
what if I had a c = 0.5? Wouldn't it be rounded incorrectly to 1.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Julia
Julia 2014년 7월 15일
편집: Julia 2014년 7월 15일
This could be due to rounding issues. It is very unlikely that you get the exact value 1 in Matlab.

Roger Stafford
Roger Stafford 2014년 7월 15일
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.)
  댓글 수: 1
Roger Stafford
Roger Stafford 2014년 7월 15일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Verification Mode에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by