Running simulation help; Left and right sides have uneven number of elements?
1 view (last 30 days)
Show older comments
Hi! I'm looking for some homework help. My task is to run a simulation on caterpillars in a tree. I have been given two equations that show the way in which the caterpillars exist in the tree.
dN = R * N(t-1) * (1 - N(t-1)/Ntot)
N(t) = N(t-1) + dN * dT;
Where:
R= rate of reproduction
Ntot=total capacity that the tree can support
N=Number of caterpillars
I am looking to run the simulation and then plot the conditions over time.
I have been given some initial conditions:
25 timesteps, interval of .1, R=5, Ntot=100 and the starting amount of caterpillars is N=10.
I have the code written out how I believe it should end up looking but I recieve the error "Unable to perform assignment because the left and right sides have a different number of elements"
I'm not really sure what part this is refering to and I don't know how to fix it. Any help appreciated!
nT=25 %timesteps
dT=.1 %interval
%variables
N=zeros(nT,1);
R=zeros(nT,1);
Ntot=zeros(nT,1);
%initial conditions
P(1)=0;
N(1)=10;
R(1)=5;
Ntot(1)=100;
%run the simulation using initial conditions and plot (without predators)
figure
hold on
for t=2:nT
dN = R * N(t-1) * (1 - N(t-1)/Ntot);
N(t) = N(t-1) + dN * dT;
plot(dN,N(t))
end
0 Comments
Answers (1)
Image Analyst
on 26 Feb 2022
Since R is a vector, dN is a vector. N is a vector but N(t) is just one element of a vector. However since dN is a vector, you have a vector on the right hand side and you're trying to put that whole vector into a single element. Can't do that. A single element at N(t) can take only a single number.
2 Comments
Image Analyst
on 28 Feb 2022
Just take the semicolons off the ends of the lines and step through it a line at a time, and then you will understand. See the link below if you don't know how to debug:
See Also
Categories
Find more on Get Started with MATLAB 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!