If statement not working for trig quad check

2 Ansichten (letzte 30 Tage)
Andrew Matteson
Andrew Matteson am 18 Sep. 2023
Kommentiert: the cyclist am 18 Sep. 2023
Hello all,
I've tried to make a simple trig quad check progam for one of my school classes, but I've ran into an error on the if statement that returns what angle my cartesian vector points to. I know that the math is correct and working as seen in the output, but the if statement is not selecting the correct value even though I'm pretty sure that the code should be.
Here is the code and output:
clear all
close all
clc
% Sample vectors
vec1 = [0.8373, 2.9192];
vec2 = [2.0376, 1.2927];
vec3 = [2.0508, -0.7287];
vec4 = [0.8720, -2.3708];
vec5 = [-1.0475, -3.0047];
vec6 = [-2.9724, -2.3876];
vec7 = [-4.1656, 0.7559];
vec8 = [-4.1700, 1.2655];
vec9 = [-2.9840, 2.9025];
vec10 = [-1.0618, 3.5280];
% Matrix with line-by-line function. For output readability
vel = [cart2polar(vec1); cart2polar(vec2); cart2polar(vec3);...
cart2polar(vec4); cart2polar(vec5); cart2polar(vec6); ...
cart2polar(vec7); cart2polar(vec8);cart2polar(vec9); ...
cart2polar(vec10)];
velT = array2table(vel,"VariableNames",{'Magnitues','Angle','Asin', ...
'pi-sin', 'cosine','2pi-cosine'});
disp(velT)
Magnitues Angle Asin pi-sin cosine 2pi-cosine _________ _______ ________ ______ _______ __________ 3.0369 0.27933 0.27933 2.8623 0.27933 6.0039 2.4131 1.0054 1.0054 2.1361 1.0054 5.2777 2.1764 0 1.2294 1.9122 1.9122 4.371 2.5261 0 0.35245 2.7891 2.7891 3.494 3.1821 3.477 -0.33545 3.477 2.8061 3.477 3.8126 4.0357 -0.89407 4.0357 2.2475 4.0357 4.2336 0 -1.3913 4.5329 1.3913 4.8919 4.3578 0 -1.2762 4.4177 1.2762 5.007 4.1628 0 -0.79924 3.9408 0.79924 5.4839 3.6843 0 -0.29234 3.4339 0.29234 5.9908
Correct values are the values of sine and cosine that match in magnitude.
Row 3 would be 1.9122
Row 7 would be 1.3913
% Function
function vec_o = cart2polar(vec_i)
% Magnitude of vector
v = norm(vec_i);
% Returns sine and cosine values
theta_s = [asin(vec_i(1)/v), pi-asin(vec_i(1)/v)];
theta_c = [acos(vec_i(2)/v), 2*pi-acos(vec_i(2)/v)];
% Combines as vector
theta = [theta_s, theta_c];
gamma = 0;
% If statement
if (theta(1) == theta(3)) && (theta(1) ~= theta(2)) && (theta(1) ~= theta(4))
gamma = theta(1);
% Quad 1
elseif theta(2) == theta(3) && (theta(2) ~= theta(1)) && (theta(2) ~= theta(4))
gamma = theta(2);
% Quad 2
elseif theta(2) == theta(4)
gamma = theta_c(2);
% Quad 3
elseif -theta(1) == theta(3)
gamma = 2*pi-theta_c(1);
% Quad 4
else
end
% Returns values
vec_o = [v, gamma, theta];
end
Thanks to anyone ahead of time
  1 Kommentar
Andrew Matteson
Andrew Matteson am 18 Sep. 2023
Comment
The 1st set of zeros under the angles column is quad 2. The 2nd set is quad 4

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

the cyclist
the cyclist am 18 Sep. 2023
Bearbeitet: the cyclist am 18 Sep. 2023
I did not look in detail, but I'd be willing to bet that the problem lies in making comparisons between floating-point numbers, which may not be represented exactly.
Try making those comparisons to within a tolerance, for example
if abs(theta(1)-theta(3)) < 1.e-9

Weitere Antworten (0)

Kategorien

Mehr zu Function Creation finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by