- converted your script to a function, cssm [attached]
- pre-allocated a couple of vectors, time and N, to improve speed
- alwaysontop and hold on
- run the function for some different input values. (I used a smaller time step.)
I am having some issues debugging my matlab program for population growth
2 次查看(过去 30 天)
显示 更早的评论
I am creating a program to show population growth based of the equation dN/dt=aN-bN^2. I am doing something wrong though because it plot a straight line starting from the intial value of the population. Here is what I have written. Any help on pointing out where I went wrong would be much obliged.
%note: N(t)= N(int) + a*N(int)*delta_t - b*N(int)^(2)*delta_t
% Clear variable %
clear all
% Prompt the user %
N_int = input('Provide a quantity for the initial number of people in a population. ');
a = input('Provide a value for the variable a: ');
b = input('Provide a value for the variable b: ');
% Declare necesary variables %
% time
time_final = ???; % I think the problem lies somewhere in declaring the variables.
delta_t = ???;
% Initialize variables %
time(1)=0;
N(1) = N_int;
% Perform calculations %
for i = 1:1:time_final
time(i+1) = time(i) + delta_t;
N(i+1) = N(i) + (a*N(i)-b*N(i)^(2))*delta_t;
end
% Plot results %
plot(time,N)
axis([0 time_final 0 4*N_int])
title('Population Growth Using Euler Integrator')
xlabel('Time')
ylabel('Population')
0 个评论
回答(2 个)
per isakson
2015-2-1
I played a bit with your equation.
0 个评论
Star Strider
2015-1-31
It seems to work, but only if you use the ‘correct’ parameters. For instance, with these, it oscillates between 5 and 10:
a = 2.5;
b = 0.3;
N_int = 10;
time_final = 50; % I think the problem lies somewhere in declaring the variables.
delta_t = 1;
So your comment to ‘time_final’ (copied here) is correct!
Experiment with them.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!