Why is only my first output value wrong?
2 views (last 30 days)
Show older comments
Mohannad Abboushi
on 5 May 2016
Commented: Image Analyst
on 6 May 2016
In this program I am modeling the the population of rabbits and foxes. The mathematical model I use predicts that each year the number of rabbits and foxes in a region change with the following equations: Δr=Ar−Brf Δf=Crf−Df where Δr and Δf are the changes in number of rabbits and foxes after one year, r and f are the number of rabbits and foxes from the previous year, and A, B, C, D are parameters describing the predator-prey interaction of the two species. Also being modeled is the movement of foxes. In the program I assume that each year, half of the foxes in each region move to the four neighboring regions (up/down/left/right regions getting 1/8th of the foxes) while the other half stay within the region. Assume this migration happens instantaneously at the end of the year, after the population of rabbits and foxes have changed due to the predator-prey interaction. Assume the foxes can move out of the country but no foxes move back into the country. Assume that the rabbits do not move across regions. Write a function rabbitsandfoxes(R,F, A,B,C,D, withmigration) that takes the matrices R and F representing the populations of rabbits and foxes from the previous year, the constants A,B,C,D, and the withmigration argument specifying whether or not to model the migration of the foxes; and returns what the number of rabbits and foxes would be after one year. If withmigration is not specified, assume it to be true.
Here's my code:
function [newR,newF]=rabbitsandfoxes(r,f,A,B,C,D,withmigration)
if ~exist('withmigration','var')
withmigration=true;
if withmigration==true
newR=abs(A.*r-B.*r.*f);
F=abs((C.*r.*f)-(D.*f))+f;
[R,C]=size(f);
newF=padarray(F/2,[1 1],'circular');
newF(1:R,2:C+1)=newF(1:R,2:C+1)+F/8;
newF(2:R+1,1:C)=newF(2:R+1,1:C)+F/8;
newF(3:R+2,2:C+1)=newF(3:R+2,2:C+1)+F/8;
newF(2:R+1,3:C+2)=newF(2:R+1,3:C+2)+F/8;
newF=newF(2:R+1,2:C+1);
elseif withmigration==false
newR=abs(A.*r-B.*r.*f)+r;
newF=abs((C.*r.*f)-(D.*f))+f;
end
end
For some reason my newR values are always off, but newF is correct. I am trying to figure out if it has something to do with calculating the equation wrong?
3 Comments
Jan
on 5 May 2016
What does "newR values are always off" exactly mean? What is "off"? Is the "end" missing in the 4th line?
Accepted Answer
Image Analyst
on 5 May 2016
It's not right because you're not migrating, and you're recursively changing newF. You're simply adding foxes.
You're not subtracting any foxes from an element to show that half of them left. You're only adding foxes. Think about it and I think you'll realize what to do (it's easy once you think about it).
Secondly you don't want to move along element by element getting a new newF value from existing newF. Think about it. If, say in row 1, you changed column 3, and then you move over to column 4 and instead of using the original value in column 3 to calculate the new value for column 4, you're using the newly changed value of column 3. You don't want to use the new value - you want to use the original value. Again, easy to solve if you just think about it.
2 Comments
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!