next up previous
Next: Dribbling Big Gulps Up: Solving the Logistic Equation Previous: Analytic Solution

Numerical Solution using MATLAB

To solve the logistic equation numerically in MATLAB we must begin by writing a function which represents the right-hand-side of the logistic equation, which the MATLAB program will then use in the numerical solution. Open an editor window in MATLAB and type in the following function:

       function ydot=logistic(t,y)
       %     right hand side of logistic equation for a matlab numerical
       %     solution.

       %     r is the intrinsic growth rate
       %     K is the carrying capacity
       
       r=.5;
       K=10;
       ydot=r*y*(1-y/K);
Now, save this as logistic.m and then back in the MATLAB command window type the following commands:
       tspan=[0 20];
       y0=1;
       [t,y] = ode45('logistic', tspan, y0);
The vector tspan you just input is the beginning and ending time for your numerical solution; above you asked for a solution starting at $t=0$ and finishing at $t=20$. You set the initial condition to be $Y_0 =1$, and the final command in the sequence is a call to MATLAB 's Fourth Order Runge-Kutta solution method, which will generate two vectors, t and y. The vector t contains increments of time which fit the time span you requested, while y contains the values of the numerical solution for $Y$ at these time increments. Thus, to plot the solution you just found you would type
       plot(t,y), xlabel('Time'), ylabel('Population Density')

 
  EXERCISE 4: Find solutions to the logistic equation for several different initial conditions and plot them simultaneously; on your composite plot also plot the carrying capacity. You may want to just give solutions for different initial conditions different names (e.g. y1, y2, y3, ...) and/or use the hold on command.  
     
 

 
  EXERCISE 5: Use MATLAB to investigate the effect of constant `harvesting' of the population, that is, to see what happens when we subtract a constant amount, $H$, of the population per time. This changes the logistic equation to

\begin{displaymath}
\frac{dP}{dt} = r P \left(1 - \frac{P}{K}\right) - H, \quad P(t=0) = P_0.
\end{displaymath}

Test several different initial conditions. Is it now guaranteed that any small initial population will grow to carrying capacity?
 
     
 


next up previous
Next: Dribbling Big Gulps Up: Solving the Logistic Equation Previous: Analytic Solution
James Powell
2002-02-15