0

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.

4
  • Neither h nor J are used within the loop, what is the purpose of repeating it? Commented Oct 21, 2013 at 0:17
  • I've scaled the script back for readability but POI is a variable defined in terms of h. I will update the post to include the full script. This may demonstrate clearer. Thanks for your time. Commented Oct 21, 2013 at 0:24
  • "Should it be within the nested loop?" What is "it"? I'm unclear what you're asking Commented Oct 21, 2013 at 0:52
  • I'm wondering if the definition of POI has to be in the nested loop or if it can remain outside. Commented Oct 21, 2013 at 1:07

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.