Inner for loop working - problems with outer for loop

I managed to advance with a problem I was having and am looking for some advice on structuring 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. Should it 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.

 Accepted Answer

I don't see POI defined anywhere - does it use global variables or something? Is it a function or a variable? Anyway, if POI does not depend on K at all, then it can be pulled out of the K loop and be assigned in the h loop. If POI is a function, then do something like
thisPOI = POI;
inside the h loop but outside the K loop, and then use thisPOI everywhere you see POI in the K loop.

6 Comments

I have updated to include the full script as POI and other variables were omitted for readability. Thanks.
I don't see how POI is supposed to change with J or K. And you no longer have an h loop.
Updated with h inside the J loop. Should POI still not reference the h value from earlier in the script/inside the J loop for the recalculation? New to MATLAB so please forgive if this sounds unclear. I've just used J as the dummy variable i.e the variable that says 'do this 6 times'.
What happens if you step through it with the debugger (like I'd have to do if I were to debug it for you)? What do you learn?
I haven't debugged it mainly because I didn't know about the debugger since i've only started using matlab recently. I was looking at the values in the variable window to try and determine what was going on. In some cases h was reaching a value in the 100's and none of my 1x11 vectors were increased to 1x66. I even laid out the calculations in microsoft Excel to cross check. Thanks.
Oh no! That's going to be a major major problem! Debugging via Answers forum discussions is a very slow way to debug a program. I suggest you take a look at http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ which will get you up to speed in just a few minutes - far faster than the hour it took me to respond to your last reply. It's something you're going to have to learn eventually anyway, so might as well take the 5 minutes now and do it. No one can get very far in MATLAB without learning how to debug their own code.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

P
P
on 20 Oct 2013

Commented:

on 21 Oct 2013

Community Treasure Hunt

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

Start Hunting!