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)

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 10일
편집: Ameer Hamza 2020년 5월 10일

1 개 추천

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

Tanner Smith
Tanner Smith 2020년 5월 10일
thanks, but I would like the while loop to continue so you can see what happens to the other equations when one of them hits zero or less. I just don't want the equation that hit a negative number to suddenly grow again. so if d goes negative i want d to stay at zero and the other equations to continue. Is this possible
Ameer Hamza
Ameer Hamza 2020년 5월 10일
Ok. There was a bit of misunderstanding. Try the updated answer.
Tanner Smith
Tanner Smith 2020년 5월 10일
thanks again for the help, this might be closer to what I am looking for. but when I try and run this code it says "Index exceeds the number of array elements (1)." or something of that sort when i input numbers that would have given me trouble. it doesn't mind numbers that were already working.
Ameer Hamza
Ameer Hamza 2020년 5월 10일
편집: Ameer Hamza 2020년 5월 10일
There was an error in the code. I have updated the code. Try it now. Also, if there is some other message, I suggest to paste the complete error message (Everything in red text)
Tanner Smith
Tanner Smith 2020년 5월 11일
thanks for the help
Ameer Hamza
Ameer Hamza 2020년 5월 11일
I am glad to be of help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2020년 5월 10일

댓글:

2020년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by