How do I limit the results of atan2() or any other options for nonlinear functions between a specific range?

8 views (last 30 days)
I am solving for output and coupler angles in a 4 bar linkage. The solution requires inverse trigonometric functions between 12 and 105 degrees. I have trive the solve() function with real number arguments and artan2(Y,X) with varying values of Y to see which quandrants might give me results. I don't believe I understand how to use artan2(), but I think this is the way.
clc;
clear;
syms f t s;
tmin=12*pi/180;
tmax=105*pi/180;
th = tmin:1:tmax;
for i = tmin:1:tmax
solutions(tmax-tmin)=psi(i);
end
plot(th,solutions);
xlabel('Theta(radians)')
ylabel('Psi(radians)')
hold on;
for i = 12:1:105
solutionf(tmax-tmin)=phi(solutions,i);
end
plot(th,solutionf);
xlabel('Theta(radians)')
ylabel('Phi(radians)')
hold on;
function [y] = phi(s,t)
a=4.5;
b=3.2;
h=7.4;
g=8.5;
y =atan2(1,(h*sin(s)-a*sin(t))/(g+b*cos(s)-a*cos(t)))-t;
end
function [x] = psi(t)
a=4.5;
b=3.2;
h=7.4;
g=8.5;
x =atan2(1,-2*a*b*sin(t)/2*b*(g-cos(t)))+acos((2*a*g*cos(t)+h^2-g^2-b^2-a^2)/sqrt((2*b*(g-cos(t)))^2+(-2*a*b*sin(t))^2));
end

Answers (1)

Walter Roberson
Walter Roberson on 2 Oct 2022
How do I limit the results of atan2() or any other options for nonlinear functions between a specific range?
Suppose you did that. Suppose that you need to restrict to 12 to 105 degrees, but that given the inputs, the angle associated with the inputs should be (for example) -18 degrees. Then you would have the following choices:
  • have the hypothetical atan2() return NaN in that position to signal that the angle is not within the desired range
  • have the hypothetical atan2() generate an error message if locations would generate an angle out of range
  • have the hypothetical atan2() return a cell array of outputs, one entry for each input value, with each entry being the appropriate angle if the angle was within the desired range, but returning an empty cell for each location that the angle would not be in the desired range
Would any of those possibilities actually improve your situation? Compared, for example, to leaving atan2() as it is, but testing the results afterwards to see if the result is in the desired range?
One thing that atan2() could not do is return an angle in a different quadrant, because that angle would not be approriate for atan2(). When you use atan2() instead of atan() you are passing the deltay and deltax information seperately, specifcally so that the arctan function can pick out the correct quadrant (since atan(y./x) by itself would not be able to tell the difference compared to atan((-y)./(-x)) but atan2(y,x) can tell the difference compared to atan2(-y,-x))
  1 Comment
Walter Roberson
Walter Roberson on 2 Oct 2022
tmin=12*pi/180;
tmax=105*pi/180;
Those are angles in radians
for i = 12:1:105
solutionf(tmax-tmin)=phi(solutions,i);
end
i appears to be an angle in degrees
tmax-tmin is a constant. If the constant happens to evaluate to a positive integer, then you would be overwriting solutions() indexed at that constant in every iteration of the loop. But the constant does not happen to be a positive integer.
Now remember that i is an angle in degrees, and it is being passed as the parameter to phi()
function [y] = phi(s,t)
and phi() receives it as t -- as what most people would expect to be a time rather than an angle
y =atan2(1,(h*sin(s)-a*sin(t))/(g+b*cos(s)-a*cos(t)))-t;
here t is being passed to sin() and cos(), which are operations on radians, but we demonstrated that t comes from i and i is an angle in degrees. So you are taking sin() and cos() of an angle expressed in degrees rather than in radians.
tmin=12*pi/180;
tmax=105*pi/180;
for i = tmin:1:tmax
tmin is 0.2094 and tmax is 1.8326 . With an increment of 1, that tmin:1:tmax is going to be generate the values 0.2094, then 1.2094, and then it would try 2.2094 but that is above 1.8326 so the loop would stop, having performed only two iterations
Here, i is an angle in radians not in degress (unlike the phi() calls) so it is acceptable that psi() uses sin(t) and cos(t)
I would suggest to you that you use either radians or degrees consistently

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!