# Stop graphing when two functions intersect

8 views (last 30 days)
Alejandro Castro on 22 Oct 2019
Edited: Adam Danz on 26 Oct 2019
I've got this program that has 2 graphs, but I want it to stop graphing when these two colide. How can do this?
%This is the first graph
for i=1:2
y = -0.0551*x + 3398.8;
grafica15 = plot(x,y,'r')
hold on
end
t = 0
n = 0
%This is the one that is building
while n<150
n = n + 1
t = 0:n
x=((vi*cos(angulo))/b)*(1-(e.^(-b*t)));
y=((1/b)*((g/b)+vi*sin(angulo))*(1-e.^(-b*t))-((g/b)*t))+(altura+15);
hold on
Grafica1 = plot(x,y)
title('Simulacion proyectiles Popocatepetl');
xlabel('Distancia Horizontal (m)');
ylabel('Distancia Vertical (m)');
str = {'Posicion en X' x(end),'Posicion en Y' y(end), 'Tiempo' t(end), 'velocidad'};
dim = [.7 .5 .3 .3];
delete(a)
a=annotation('textbox',dim,'String',str,'FitBoxToText','on');
drawnow;
pause(1)
grid on
end

Adam Danz on 22 Oct 2019
Edited: Adam Danz on 26 Oct 2019
I'd calculate the index where the curves intersect using intersections() (download it from the file exchange) and I'd do this prior to your plotting loops. Then you'd know where to stop ahead of time.
BTW, there's a lot of places in your code that you're repeating the same unnecessary steps and that consumes much time. For example, there's no need to set the title and axis labels on each iteration. That stuff can be moved outside of the loop. For example,
axes()
hold on
title(. . .)
xlabel(. . .)
ylabel(. . .)
grid on
while n<150 % <-- stop at the index value of the intersection.
end