if statements inside of while loop

6 visualizaciones (últimos 30 días)
Tanner Smith
Tanner Smith el 10 de Mayo de 2020
Comentada: Ameer Hamza el 11 de Mayo de 2020
I would like to run this while loop for a inputted number of cycles. but if the input's at any point through out those cycles cause one of my equations to output to be <= 0 then I would like that equation to just output 0 for then on. I think that can be done with an if statement between each of my top 3 eaquations but I'm not sure how to do it. any help would be appreciated.
% Predator-Prey Model Equations
clear,clc
% variables, we have to set these at the begin
d0=input('Enter Starting population of Deer: ');
m0=input('Enter Starting population of Moose: ');
w0=input('Enter Starting population of Wolves: ');
y=input('Enter Number of years to graph: ');
i=1;
k=1;
%fixed rates
a=.0035; % Deer repop rate
b = .0004; % Rate wolves find deer
c=.0029; % Moose repop rate
e=.00039; % rate wolves find moose
f=.15; % wolves effiecency at making food into offspring (repop rate)
g = .07; % Death rate of wolves
%h=.05; % an added death rate that could be used if you wanted to add in
%deaths from people or other animals
%equations
% D=d0+d0^2*A-B*d0^2*w0-d0^2*H;
% M=m0+m0^2*C-E*m0^2*w0-m0^2*H;
% W=w0+w0^2*F-w0^2*G;
%plot %
% here is the general idea for the plot these equations don't have the
%extra death rate like we talked about, those can be added
while k<=y
d(i)=d0+((d0^2)/2)*a-((d0^2)/2)*b*w0; % a=Deer repop rate b=rate wolves find deer
m(i)=m0+((m0^2)/2)*c-((m0^2)/2)*e*w0;% c= Moose repop rate e= rate wolves find moose
w(i)=w0+((w0^2)/2)*f*(m0*b+d0*e)-g*w0; %f= wolves effiecency at making food into offspring g= death rate of wolves
d0=d(i);
m0=m(i);
w0=w(i);
i=i+1;
k=k+1;
end
%plotting
plot(1:i-1, d, 'o');
title('Deer Over Time')
grid on
axis([0, i, 0, 100000])
plot(1:i-1, m, 'o')
grid on
axis([0, i, 0, 100000])
title('Moose over time')
plot(1:i-1, w, 'o')
grid on
axis([0, i, 0, 25])
title('wolves over time')
%display final outputs
disp('Final amount of deer')
disp(d0)
disp('Final amount of Moose')
disp(m0)
disp('Final amount of Wolves')
disp(w0)

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 10 de Mayo de 2020
Editada: Ameer Hamza el 10 de Mayo de 2020
You can just break the loop at that point.
flags = false(1,3);
while k<=y
if flags(1)
d(i) = 0;
else
d(i)=d0+((d0^2)/2)*a-((d0^2)/2)*b*w0; % a=Deer repop rate b=rate wolves find deer
if d(i) < 0
flags(1) = true;
end
end
if flags(2)
m(i) = 0;
else
m(i)=m0+((m0^2)/2)*c-((m0^2)/2)*e*w0;% c= Moose repop rate e= rate wolves find moose
if m(i) < 0
flags(2) = true;
end
end
if flags(3)
w(i) = 0;
else
w(i)=w0+((w0^2)/2)*f*(m0*b+d0*e)-g*w0; %f= wolves effiecency at making food into offspring g= death rate of wolves
if w(i) < 0
flags(3) = true;
end
end
d0=d(i);
m0=m(i);
w0=w(i);
i=i+1;
k=k+1;
end
  6 comentarios
Tanner Smith
Tanner Smith el 11 de Mayo de 2020
thanks for the help
Ameer Hamza
Ameer Hamza el 11 de Mayo de 2020
I am glad to be of help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by