Help troubleshooting iteration code
Show older comments
Hi,
I have an iteration code which I am using to find latitude/longitude of a set of heights (h_intercept). This is a 1x79 matrix.
It works perfectly until the 22nd value. I've found that this is when h_test>h_intercept. I tried to put a condition in to reset it but it doesn't work.
Furthermore, the strange thing is that the iteration runs and finds the height - the issue is that it is taking the wrong height.
For example
h_intercept=sat_look_pass1_llh(3,60:79)/2e3;
for j=1:length(h_intercept)
rng_sat= sat_look_tcs_pass1(3,j);
u_sat=[sat_look_tcs_pass1(1,j)/sat_look_tcs_pass1(3,j);sat_look_tcs_pass1(2,j)/sat_look_tcs_pass1(3,j);sat_look_tcs_pass1(3,j)/sat_look_tcs_pass1(3,j)];
h_intercept=sat_look_pass1_llh(3,j)/2e3;
h_test=zeros(1,3);
rng_test_min=0;
rng_test_max=rng_sat/2e3;
err=0.01;
while abs(h_intercept-h_test)>err
rng_test=(rng_test_min+rng_test_max)/2;
tcs_test=u_sat*rng_test;
llh_test=tcs2llhT(tcs_test,station_llh);
h_test=llh_test(3,:);
if h_test>=h_intercept
rng_test_max=rng_test;
else
rng_test_min=rng_test;
end
end copter_llh(:,j)=(llh_test); h_interceptloop(:,j)=(h_intercept); end % code end
This code is using values 60:79 of my matrix and although it runs, it is actually finding h_intercept(1,1:19)
It seems that after h_test>h_intercept, it starts from the beginning of the height matrix. How is that possible?!
Any suggestions appreciated!
Answers (2)
Image Analyst
on 11 Sep 2014
1 vote
Here's a virtually foolproof way to solve it http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
Guillaume
on 11 Sep 2014
You start with this line:
h_intercept=sat_look_pass1_llh(3,60:79)/2e3;
which as you say uses columns 60 to 79 of your sat_look_pass1_llh matrix. However, in the for loop, you overwrite it with:
h_intercept=sat_look_pass1_llh(3,j)/2e3;
with j from 1 to 19 (length(h_intercept)). Thus you're iterating over columns 1 to 19. As it is the first line serves no purpose.
I suspect you wanted to do:
h_intercepts=sat_look_pass1_llh(3,60:79)/2e3;
for j = 1:length(h_intercepts)
h_intercept = h_intercepts(j);
...
end
which you could also simplify with
h_intercepts=sat_look_pass1_llh(3,60:79)/2e3;
for h_intercept = h_intercepts
...
end
4 Comments
Buzz
on 11 Sep 2014
you set h_test to 1x3 zero's with this line
h_test = zeros(1,3);
Are you expecting h_intercept to be negative? Either way in your while loop you then overwrite h_test with
h_test = llh_test(3,:);
Have you checked what it is that these variables contain.
h_test is an array and h_intercept is 1x1 - is this correct?
You need to use the debugger as image analyst suggests to check your variables are doing what you expect them to do. I tend to work out the loop by hand first to check what it is I should be getting (certainly when it is relatively simple like this one anyway).
Keyboard is one way to get started with debugging.
Buzz
on 11 Sep 2014
Buzz
on 11 Sep 2014
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!