0
for j = 1:4
    n = (co(e(j,2),:) - co(e(j,1),:));
    n = n./norm(n);
    F = e(j,5).*e(j,4)./e(j,3).*-(dot(u,n)).*n
    theta = acosd(dot(F,n)/(norm(F)*norm(n)))

    if theta == 180
        F2 = [F2; -sqrt(F(1)^2 + F(2)^2 + F(3)^2)]
    else if theta == 0
            F2 = [F2; sqrt(F(1)^2 + F(2)^2 + F(3)^2)]
        end
    end
end

Essentially, what the issue is that the first value of F (for j = 1) in the if statement is being ignored, even though the value of theta is 180. To check, I've done 'whos theta' and even wrote theta == 180 into the code for which it returned 0. For j = 2:4 though, the code works absolutely fine despite j = 2 yielding a theta value of 180 as well. It's almost as if it's skipping the if statement entirely for j = 1. I believe this is linked to the fact that F is the only matrix vector with three non-zero components, but still don't know how to go about solving this.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
Jarmuh
  • 3
  • 1
  • If theta == 180 yields 0, then theta is not 180. The issue isn't with the if-statement, but the value assignment to theta (or F). I recommend looking at F and theta individually when j = 1 – Brendan Frick Nov 07 '16 at 18:10

1 Answers1

0

theta might a be double and very close to 180 but not exactly 180.00000001 ~= 180. It might be better to compared with a given tolerance.

tol = 1e-3;
if abs(theta-180)<tol
...
end

Matlab also has internal floating-point accuracy eps, it might be an interesting read.

Additionally you 'else-if' statement should elseif but that might be just a typo from copy-paste.

mpaskov
  • 1,234
  • 2
  • 11
  • 21
  • For more on the dangers of using `==` and `~=` to compare floating point numbers, see [this question](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) and the links therein. – craigim Nov 07 '16 at 18:16