If statement not returning anticipated output

4 views (last 30 days)
Purpose is to allow s1 to be printed as positive bearing in the 360 degree coordinate system. However, when s1 is printing as it's negative counterpart and it seems the if statement is being completely skipped. Beyond this, even if the statement is rewritten to evaluate if the variable sad is greater than 0 the result is still the same. It is probably trivial but I am stumped.
if sad < 0
s1 = sad + 360;
else
s1 = sad;
end
and disp(s1) returns -140 instead of the anticipated 220

Answers (1)

Star Strider
Star Strider on 20 Jun 2014
Edited: Star Strider on 20 Jun 2014
There must be something elsewhere in your code that’s causing the problem.
This works fine for me:
sad = -140;
if sad < 0
s1 = sad + 360;
else
s1 = sad;
end
produces:
s1 =
220
For sad = 140, s1 = 140.
  6 Comments
Star Strider
Star Strider on 20 Jun 2014
We’re missing wiv, and I don’t completely understand the complexities of Stoke’s drift on a brief reading of the Wikipedia article, except that it seems to involve wave dynamics.
If you are simply interested in the velocity vector that results from the interaction of current and wind vectors on something floating on the surface (and neglecting the wave dynamics, drag, and other complications), so the resulting direction is the vector sum of these two velocity vectors, this example may do what you want. I did not do any magnetic-to-true conversions, assuming that you do them before you get this far. I included a compass plot to visualise the result:
wd = randi(360, 1, 9); % CREATE WIND DIRECTIONS, VELOCITIES
wv = randi(30, 1, 9);
cd = randi(360, 1, 9); % CREATE CURRENT DIRECTIONS, VELOCITIES
cv = randi(5, 1, 9);
wx = wv .* cosd(wd); % CONVERT TO CARTESIAN
wy = wv .* sind(wd);
cx = cv .* cosd(cd);
cy = cv .* sind(cd);
rx = wx + cx; % CALCULATE RESULTING VELOCITY (CARTEISIAN)
ry = wy + cy;
rd = atan2d(ry,rx); % RESULTING DIRECTIONS (POLAR)
rd360 = rd;
rd360(rd360<0) = rd360(rd360<0) + 360; % 360° CORRECTION
rv = hypot(rx,ry); % RESULTING VELOCITIES (POLAR)
figure(1) % PLOT TO CHEC ACCURACY
for k1 = 1:length(wd)
subplot(3,3,k1)
compass([wx(k1) cx(k1)], [wy(k1) cy(k1)], '-b') % ORIGINAL X,Y COORDINATES
hold on
rxp(k1) = rv(k1)*cosd(rd360(k1)); % CALCULATE RESULTING X,Y FROM ‘rd360’ & ‘rv’
ryp(k1) = rv(k1)*sind(rd360(k1));
compass(rxp(k1), ryp(k1), '-r')
hold off
title(sprintf('W(%.1f, %.1f), C(%.1f, %.1f), R(%.1f, %.1f)', wd(k1),wv(k1),cd(k1),cv(k1),rd360(k1),rv(k1)), 'FontName','Candara', 'FontSize',7)
end
I used nine-element vectors because they fit neatly on the subplot.
Geoff Hayes
Geoff Hayes on 20 Jun 2014
@Thomas - it would be interesting to see what the negative sad value is before the code encounters the if statement and adds the 360 to it. Try changing the disp(s1) to disp([sad s1]). It may be, for this example, that sad is -500.9761.
If that is the case, then a while loop may be needed instead:
while sad<0
sad = sad + 360;
end
s1 = sad;

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!