Problem with Nested Plotting Program.

1 view (last 30 days)
Kyle Etters
Kyle Etters on 7 Aug 2011
Hi.
I'm having problems with an m-file that no longer works as it once did.
Main Program:
%Hyperbola looping program
function hyperbola_plot
%Arrays with station locations
S1 = [-0.3,7.2];
S2 = [6.1,12.6];
S3 = [16.6,15.0];
S4 = [33.0,12.6];
S5 = [37.1,13.2];
S6 = [48.1,12.0];
S7 = [43.5,13.3];
S8 = [39.2,14.9];
S9 = [30.1,17.1];
S10 = [23.0,20.5];
S11 = [17.0,21.5];
S12 = [13.3,19.5];
S13 = [8.0,20.7];
S14 = [4.0,16.9];
S15 = [40.7,18.0];
S16 = [31.5,25.0];
S17 = [24.5,26.0];
S18 = [23.0,31.2];
S19 = [31.5,32.0];
S20 = [15.7,35.0];
S21 = [19.9,39.0];
S22 = [15.8,29.5];
S23 = [12.5,36.8];
%Hyperbola sets to plot.
hyperbola_loop(S1(1),S1(2),S2(1),S2(2));
hyperbola_loop(S2(1),S2(2),S14(1),S14(2));
hyperbola_loop(S2(1),S2(2),S13(1),S13(2));
hyperbola_loop(S14(1),S14(2),S13(1),S13(2));
hyperbola_loop(S13(1),S13(2),S12(1),S12(2));
hyperbola_loop(S12(1),S12(2),S3(1),S3(2));
hyperbola_loop(S12(1),S12(2),S11(1),S11(2));
hyperbola_loop(S11(1),S11(2),S10(1),S10(2));
hyperbola_loop(S10(1),S10(2),S9(1),S9(2));
hyperbola_loop(S9(1),S9(2),S4(1),S4(2));
hyperbola_loop(S4(1),S4(2),S5(1),S5(2));
hyperbola_loop(S9(1),S9(2),S5(1),S5(2));
hyperbola_loop(S5(1),S5(2),S8(1),S8(2));
hyperbola_loop(S8(1),S8(2),S15(1),S15(2));
hyperbola_loop(S5(1),S5(2),S15(1),S15(2));
hyperbola_loop(S8(1),S8(2),S7(1),S7(2));
hyperbola_loop(S7(1),S7(2),S15(1),S15(2));
hyperbola_loop(S7(1),S7(2),S6(1),S6(2));
hyperbola_loop(S9(1),S9(2),S16(1),S16(2));
hyperbola_loop(S10(1),S10(2),S17(1),S17(2));
hyperbola_loop(S17(1),S17(2),S16(1),S16(2));
hyperbola_loop(S17(1),S17(2),S18(1),S18(2));
hyperbola_loop(S16(1),S16(2),S17(1),S17(2));
hyperbola_loop(S18(1),S18(2),S19(1),S19(2));
hyperbola_loop(S18(1),S18(2),S20(1),S20(2));
hyperbola_loop(S20(1),S20(2),S21(1),S21(2));
hyperbola_loop(S3(1),S3(2),S11(1),S11(2));
hyperbola_loop(S3(1),S3(2),S10(1),S10(2));
hyperbola_loop(S2(1),S2(2),S3(1),S3(2));
hyperbola_loop(S11(1),S11(2),S22(1),S22(2));
hyperbola_loop(S17(1),S17(2),S22(1),S22(2));
hyperbola_loop(S22(1),S22(2),S18(1),S18(2));
hyperbola_loop(S22(1),S22(2),S20(1),S20(2));
hyperbola_loop(S22(1),S22(2),S23(1),S23(2));
hyperbola_loop(S23(1),S23(2),S20(1),S20(2));
hyperbola_loop(S23(1),S23(2),S21(1),S21(2));
hyperbola_loop(S18(1),S18(2),S21(1),S21(2));
hyperbola_loop(S17(1),S17(2),S19(1),S19(2));
hyperbola_loop(S9(1),S9(2),S15(1),S15(2));
hyperbola_loop(S15(1),S15(2),S6(1),S6(2));
hyperbola_loop(S6(1),S6(2),S8(1),S8(2));
Here's the code for hyperbola_loop:
%Hyperbola looping program
function hyperbola_loop(x1,y1,x2,y2)
for i = -9:0.1:9
hyperbola(-2,2,x1,y1,x2,y2,i);
end
The hyperbola function is a custom function based on a routine written by another Matlab user. It draws hyperbolas based on the time-of-arrival problem. Details in the file.
%Hyperbola Drawing Program (single-branch)
function hyperbola(t1, t2, x1, y1, x2, y2, d)
%t1 and %t2 are the endpoints of the parametric parameter used in this
%%program
%x1 and y1 are the coordinates of the first focus.
%x2 and y2 are the coordinates of the second focus.
%a is the semi-transverse axis (1/2 of tranverse axis length)
%b is the semi-conjugate axis (1/2 of conjugate axis length)
%c is 2x distance between the foci
%d = 2a is the difference in the distances between the foci and a point on
%%the hyperbola. d cannot be greater than the distance between foci.
t = linspace(t1,t2); % Generate parameter values between t1 and t2
c = sqrt((x2-x1)^2+(y2-y1)^2)/2;
a = d/2; b = sqrt(c^2-a^2);
X = a*cosh(t); Y = b*sinh(t); % Hyperbola branch in standard form
ca = (x2-x1)/(2*c); sa = (y2-y1)/(2*c); % Cosine & sine of rot. angle
x = (x1+x2)/2 + X*ca - Y*sa; % Rotated & translated hyperbola
y = (y1+y2)/2 + X*sa + Y*ca;
plot(x,y);
%Code courtesy of Roger Stafford
%http://www.mathworks.in/matlabcentral/newsreader/view_thread/158418
As far as I can tell by the time of this writing, the hyperbola custom function works as intended, and the hyperbola_loop function works when run once. The problem is that I need to run it over and over because I have a lot of hyperbolas to plot. Hence the need for the hyperbola_plot function. What this function is supposed to do is plot a set of hyperbolas between two fixed foci, for many foci sets.
I sometimes get what I'm looking for (a set of hyperbolas). Other times, I just get one plotted hyperbola. I've tried setting hold to 'on' or 'all' before running hyperbola_plot, but that does nothing. I've made sure to run hyperbola_plot while in the proper Matlab directory, but that still doesn't reign in the end result.
What is going on here?
EDIT: I'm running this code using Matlab 7.40.
  1 Comment
Oleg Komarov
Oleg Komarov on 7 Aug 2011
Have you created axes before you run any plotting function and then called hold on?

Sign in to comment.

Answers (2)

Jan
Jan on 7 Aug 2011
You "sometimes" get, what you are looking for, "other times" you get one line only. Then it would be important to explain, what the difference between these two cases is. MATLAB is a deterministic machine - this means, that different results must be based on different inputs. We do not have any chance to guess, what you've made differently, but are able to know it.
Please try this before calling your plot function:
figure;
axes('NextPlot', 'add')) % Equivalent to "hold('on')"

Kyle Etters
Kyle Etters on 8 Aug 2011
The only time input has changed before this error is adding more calls of hyperbola_loop in the hyperbola_plot m file.
I tried your suggestion, and it didn't do any good. In fact, I obtained no lines and a messy axes. Honestly, I've never had to manually turn on the axes. It plots axes just fine.
I've placed a hold('on'); command before the multiple calls in the hyperbola_plot function, and I'm finally getting the multiple plots I was looking for. Hopefully using the hold('on') function in the program was the trick. I'll know soon enough.

Community Treasure Hunt

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

Start Hunting!