I'm having trouble plotting a population vs time model; I keep getting the error "vectors must be the same length." I need my x-axis to show population from 1804-2300, but I had to use 1-496 as my time values in the code. I think linspace needs to come into play somewhere, but it doesn't seem to be working. Note: there very well may be other issues in my code. Thanks in advance for your help!
%Variable key
%t is the number of years the model runs, from 1804-2300.
t = [1:496]
%r is the population growth rate.
r = 0.01
%K is the human carrying capacity in billions of people.
K = 10
%P0 is the human population in billions at the time the model begins.
P1 = 1
%A is the
A = (K-P1)/P1
%P is the human population in the model in billions of people.
P = (K)./(1+A*exp(-r*t))
%Let 1804, the year the model begins, equal year 1.
t(1) = 1
%Loop ensures that the the population data cycles through the model for every year and stops 496 years after the model begins, at 2300.
for i = 1:496
if t(i) <= 496
% Population growth (pop_g)
r = 0.01;
else
break
end
%If-else clause takes into account the human carrying capacity of 10 billion by lowering the growth rate after the carrying capacity is reached.
if P <=10
r = 0.01
else
r = .008
end
%Calculate the next year of population growth using the population that was just aquired.
P(i+1) = (r*P(i)) + P(i)
end
%Plot the population model data.
figure
hold on
t = linspace(1804,2300)
plot (t,P)
title ('Model Population Test Graph')
legend ('Model Population')
xlabel ('Years')
ylabel ('Population')
grid on

 Réponse acceptée

KSSV
KSSV le 28 Fév 2019
Modifié(e) : KSSV le 28 Fév 2019

0 votes

To plot the xdata and ydata should be same. In your case t and P are of different size. Get them to same size.
%Variable key
%t is the number of years the model runs, from 1804-2300.
t = [1:496] ;
%r is the population growth rate.
r = 0.01 ;
%K is the human carrying capacity in billions of people.
K = 10 ;
%P0 is the human population in billions at the time the model begins.
P1 = 1 ;
%A is the
A = (K-P1)/P1 ;
%P is the human population in the model in billions of people.
P = (K)./(1+A*exp(-r*t)) ;
%Let 1804, the year the model begins, equal year 1.
t(1) = 1;
%Loop ensures that the the population data cycles through the model for every year and stops 496 years after the model begins, at 2300.
for i = 1:496
if t(i) <= 496
% Population growth (pop_g)
r = 0.01;
else
break
end
%If-else clause takes into account the human carrying capacity of 10 billion by lowering the growth rate after the carrying capacity is reached.
if P <=10
r = 0.01 ;
else
r = .008 ;
end
%Calculate the next year of population growth using the population that was just aquired.
P(i+1) = (r*P(i)) + P(i) ;
end
%Plot the population model data.
figure
hold on
t = linspace(1804,2300,length(P)) ;
plot (t,P)
title ('Model Population Test Graph')
legend ('Model Population')
xlabel ('Years')
ylabel ('Population')
grid on

2 commentaires

Amelia Baran
Amelia Baran le 28 Fév 2019
Thank you so much!
Square brackets are superfluos:
t=1:496;

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by