I am having some issues debugging my matlab program for population growth

1 visualización (últimos 30 días)
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')

Respuestas (2)

per isakson
per isakson el 1 de Feb. de 2015
I played a bit with your equation.
  • 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.)

Star Strider
Star Strider el 31 de En. de 2015
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.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by