Trouble with vector length
显示 更早的评论
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
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!