Loop results in an array

1 Ansicht (letzte 30 Tage)
Dude
Dude am 8 Sep. 2016
Bearbeitet: mbonus am 8 Sep. 2016
I've created a loop to calculate the number of rabbits in population after 7 iterations (initial population = 2) with a growth rate of 0.8.
while dt <= 7
totalRabbits = initialRabbits + (initialRabbits * b * dt)
initialRabbits = totalRabbits
dt = dt+1
end
The next thing I need to do is plot the time (1-7) against the population at each time. I'd like to put these values into an array. How can I do this?

Antworten (1)

mbonus
mbonus am 8 Sep. 2016
Bearbeitet: mbonus am 8 Sep. 2016
timevector = [1:7];
rabbitvector = zeros(7,1);%change the value of 7 in the arrays to allow more time steps
dt = 1;
while dt <= 7
totalRabbits = initialRabbits + (initialRabbits * b * dt)
initialRabbits = totalRabbits;
rabbitvector(dt) = totalRabbits;
dt = dt+1
end

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by