I am mistaken for angle

1 view (last 30 days)
Muhendisleksi
Muhendisleksi on 2 Mar 2017
Commented: Muhendisleksi on 2 Mar 2017
Dx=[451.1489;71.922;22.064;-52.849;-434.247] Dy=[423.253;290.384;292.965; 296.519;377.443]
[u m]=size(Dx);
for i=1:u t(i)=atan(Dy(i)/Dx(i))*200/pi; end
if Dx(i)>0 & Dy(i)>0 t(i)=t(i) elseif Dx(i)<0 & Dy(i)>0 t(i)=t(i)+200 elseif Dx(i)<0 & Dy(i)<0 t(i)=t(i)+200; elseif Dx(i)>0 & Dy(i)<0 t(i)=t(i)+400; end
The result is wrong; t= [47.970; 84.543; 95.214; -88.771; 154.448]
The correct result is; t= [47.970; 84.543; 95.214; 111.229; 154.448]
Is line 4 causing the error?

Accepted Answer

Jan
Jan on 2 Mar 2017
Edited: Jan on 2 Mar 2017
Note that you change the result outside the loop:
for i=1:u
t(i)=atan(Dy(i)/Dx(i))*200/pi;
end % <-- remove this
if Dx(i)>0 & Dy(i)>0
t(i)=t(i) % Omit this one :-)
elseif Dx(i)<0 & Dy(i)>0
t(i)=t(i)+200
elseif Dx(i)<0 & Dy(i)<0
t(i)=t(i)+200;
elseif Dx(i)>0 & Dy(i)<0
t(i)=t(i)+400;
end
% end % <-- insert an "end" here
When processed after the loop, only the last value of "i" is accessed.
Logical indexing will be more efficient:
t = atan(Dy ./ Dx) * (200/pi); % elementwise: ./
index = (Dx<0 & Dy>0);
t(index) = t(index) + 200;
and so on.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!