Stop graphing when two functions intersect

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

回答(1 个)

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

类别

帮助中心File Exchange 中查找有关 Axis Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by