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개)

Adam Danz
Adam Danz 2019년 10월 22일
편집: Adam Danz 2019년 10월 26일

0 개 추천

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에 대해 자세히 알아보기

태그

질문:

2019년 10월 22일

편집:

2019년 10월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by