Why is only my first output value wrong?

1 次查看(过去 30 天)
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 个评论
Jan
Jan 2016-5-5
What does "newR values are always off" exactly mean? What is "off"? Is the "end" missing in the 4th line?
Mohannad Abboushi
For instance if [newR,newF]=rabbitsandfoxes[0 0 0; 0 1000 0; 0 0 0], [0 0 0; 0 100 0; 0 0 0], .5, .01, .01, .2, true
I get: { 0 0 0 0 1500 0 0 0 0, 0 135 0 135 540 135 0 135 0 } when it should be: [0 0 0; ... 0 500 0; ... 0 0 0], [0 135 0; ... 135 540 135; ... 0 135 0]

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2016-5-5
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 个评论
Image Analyst
Image Analyst 2016-5-6
Yes. That was also one of the errors along with some others.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by