While loop stops short of meeting the condition
Show older comments
I have a loop function which is supposed to find the x=0 intercept of two trigonometric functions x=x(t). The two functions alter depending on whether the x is greater or lower than 0. Although I'm using the same while loop for both x(t)>0 and x(t)<0 domains, the while loop unexpectedly stops short of reaching x(t)=0 for only x(t)<0 intervals of the function. That is, only one half of the same while loop works properly.
Please forgive me for not uploading the complete code - I have half a dozen .m files, and also there is an issue of intellectual property (I'm not the owner, just a collaborator). I will try to explain the best I can what the problem is.
The subfunction and the while loop are as follows:
function[x] = get_x(f_dn, X, phi, wndn, wddn, zetadn, ttemp)
if f_dn == 'u'
x = X*cos(wndn*ttemp-phi);
elseif f_dn == 'd'
x = X*exp(-zetadn*wndn*ttemp)*cos(wddn*ttemp-phi);
else fprintf('Incorrect f_up/f_dn entry (should be U or D).');
end
end
ttemp = 1.05*t; % t is the lower limit of the interval, where x(t)=0,...
% already provided as an argument
xapproach = 0.001;
if rem(m,2) == 0 % even interval
xup = xapproach;
xdn = -xapproach;
else
xup = xapproach;
xdn = -xapproach;
end
while (abs(x) > xapproach)
if rem(m,2) == 0 % even interval
if x > xup
ttemp = ttemp + abs(x)*ttemp/m;
elseif x < xdn
ttemp = ttemp - abs(x)*ttemp/m;
end
elseif rem(m,2) ~= 0 % odd interval
if x < xup
ttemp = ttemp + abs(x)*ttemp/m;
elseif x > xdn
ttemp = ttemp - abs(x)*ttemp/m;
end
else
disp('Some error.');
end
x = get_x(f_dn, X, phi, wndn, wddn, zetadn, ttemp)
end
Arguments such as X, phi, etc. are inherited from other functions, defined in other .m files.
The issue is visible from the plot:


As you can tell, each odd (red) interval starting from the third one onwards ends prematurely, which in turn affects the following even (blue) interval's start. At the same time, even (blue) intervals complete just fine, and I am able to find the x(t)=0 upper limits of those intervals just fine. But both of these are the part of the same while loop. I'm really confused as to why is this happening.
Let us look at a typical command window print of x for the odd (red) interval:
x = -0.1280
x = -0.2757
x = -0.5566
x = -0.8112
x = -0.1367
x = 0.0759
x = -0.0463
x = 0.0270
x = -0.0162
x = 0.0096
x = -0.0057
x = 0.0034
x = -0.0020
x = 0.0012
So from what I can tell my function starts right, it sets a point on the odd (red) interval so that x(t)<0, then starts moving the point right along the red line. If it overshoots, it corrects and moves the point to the left by an increasingly smaller increment. However, it ultimately suddenly stops at x = 0.0012, where the condition abs(x) > xapproach is still ans = 1. Increasing the precision of xapproach produces the same result. As noted, the very same while loop finds the upper limit x(t)=0 of the blue interval just fine, as you can alo see from the plot.
Any ideas on what I can investigate in more detail to understand what the issue is? Quite a few similar questions here had an issue of long precision, but I do not see that being an issue here (again, the same while loop, etc.) Thank you in advance.
2 Comments
Ghazwan
on 12 Oct 2022
Given the provided information, it looks like it is an issue of rounding the double numbers when it is approaching zero. A possible solution is that you equate the outputs when reaching these points. If not, increasing the number of calculating points would reduce the gap.
Milos Krsmanovic
on 19 Oct 2022
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!