自分で書いた関数に問題があります。自分で解決する方法を質問で見つけられなかったので、誰かが私が見ていないものを見ることを期待してここに投稿します...(これは私の最初の質問であり、フォーマットする方法がわかりませんMatlabコード、ごめんなさい)
これは私の関数で、「壁」(AB で指定) 上の光線 (セグメント CD で指定) の入射角を計算します。AB (壁) は水平または垂直であると想定され、セグメント CD と壁 AB には交差があると想定されます。
function angle = incidentAngle(AB,CD)
%AB and CD are two vectors of size 1x4
%AB = [a1 a2 b1 b2] et CD = [c1 c2 d1 d2]
%AB is a segment from the point (a1,b1) to the point (a2,b2)
if(AB(1)==AB(3)) % The wall is vertical
if(CD(1)==CD(3)) % The "ray" is vertical too, the angle is 90° or pi/2
angle = pi/2;
else
angle = abs(atan((CD(2)-CD(4))/(CD(1)-CD(3))));
end
else
if(AB(2)==AB(4))% The wall is horizontal
if(CD(2)==CD(4)) % The "ray" is horizontal too, the angle is 90° or pi/2
angle = pi/2;
else
angle = abs(atan((CD(1)-CD(3))/(CD(2)-CD(4))));
end
end
end
end
問題は(たとえば)次のとおりです。
ABとCDを次のように定義すると
AB = [0 1 0 5];
CD = [-1 2 3 4];
そして私がすること
angle = incidentAngle(AB,CD)
コマンド ウィンドウにエラー メッセージが表示されます。
??? Subscript indices must either be real positive integers or logicals.
そして、私はその理由を理解していません...
次のように、コマンドウィンドウで関数をコピーするだけの場合:
AB = [0 1 0 5];
CD = [-1 2 3 4];
angle = 0;
if(AB(1)==AB(3)) % The wall is vertical
if(CD(1)==CD(3)) % The "ray" is vertical too, the angle is 90° or pi/2
angle = pi/2;
else
angle = abs(atan((CD(2)-CD(4))/(CD(1)-CD(3))));
end
else
if(AB(2)==AB(4))% The wall is horizontal
if(CD(2)==CD(4)) % The "ray" is horizontal too, the angle is 90° or pi/2
angle = pi/2;
else
angle = abs(atan((CD(1)-CD(3))/(CD(2)-CD(4))));
end
end
end
angle
正しい答えが得られ、
angle =
0.4636