loco
loco

Reputation: 321

Inner for loop working - outer loop incorrect?

I managed to advance with a problem I was having and am looking for advice on the structure of my outer for loop. My inner loop works correctly. I want to repeat the 11 calculations 6 times using a different height (value of h) for each iteration. On the iterations of the J loop h increases to it's original value + 2*h. POI in the K loop depends on h and is defined but has been omitted for readability. Can update with the full script if necessary. Should the definition of POI be within the nested loop?

h = 0.5985;
GroundDistance = 18.75;
SouthAngle = 57;
TreeHeight = 14;
POI = TreeHeight-h;
BuildingElevationAng = atand(POI/GroundDistance);
a=0.265;
TreeLengthRHS = 15.89+a;
h = 0.5985;

    X(1,1)=h;
    for k = 2:6;
      X(k) = X(k-1) + (X(1)*2);
    end

for J = 1:6
  h=h+(2*h);
     for K  = 1:11
       TreeLengthTest(K) = TreeLengthRHS + (2*(K-1)*a);
       TreeLengthLHS = 75 - TreeLengthTest(K);
       AngleBx(K) = atand(TreeLengthLHS/GroundDistance);
       AngleCx(K) = atand(TreeLengthTest(K)/GroundDistance); %wasTreeLengthRHS
       DistanceAx(K) = GroundDistance/cosd(SouthAngle);
       DistanceBx(K) = GroundDistance/cosd(AngleBx(K));
       DistanceCx(K) = GroundDistance/cosd(AngleCx(K));
       AltAngleA(K) = atand(POI/DistanceAx(K));
       AltAngleB(K) = atand(POI/DistanceBx(K));
       AltAngleC(K) = atand(POI/DistanceCx(K));
       AzimuthA = 0;
       AzimuthB = (-AngleBx)-SouthAngle;
       AzimuthC = AngleCx-SouthAngle;
       end
end

Any help is greatly appreciated.

Upvotes: 0

Views: 187

Answers (1)

darthbith
darthbith

Reputation: 19597

Well, there are a couple things I'm not sure of, but I'll give a shot at an answer, and see if this helps. The definition of POI does not have to be inside the inner loop, because POI is constant for each of those 11 calculations. However, I think you're defining h a little bit incorrectly, because you'll never get to use the value of h that you define initially. Since you already have the X vector, what if you try your nested for loops like:

for J = 1:6
    POI = TreeHeight - X(J);
    for K = 1:11
        ...All your code here...
    end
end

Upvotes: 1

Related Questions