Often it is not possible or desirable to solve a differential equation,
For example, suppose you want to solve the exponential growth equation for
aphids, but include a constant `harvesting' rate due to predation by
ladybird beetles, as in
% variables for the discretization: tmax=10; % set the time to finish solving N=100; % set number of time intervals dt=tmax/N; % determine delta t t=linspace(0,tmax,N+1); % not required for the DE, but useful for % plotting % set parameters for the ODE: h=.2; % harvesting rate r=.1; % growth rate p(1)=3; % set the initial population p=zeros(1,N+1); % define an array to hold the solution for n=1:N % begin loop for Euler solution p(n+1)=(1+r*dt)*p(n)-h*dt; end % end loop for Euler solution plot(t,p) % plot the solution
Save this script in an m-file and run it to see how solutions look.
The differential equation has a steady state at
; can you
confirm this numerically? Is the steady state stable or unstable? In this
particular case we know the analytic solution; as we calculated in class
pactual=h/R+(p(1)-h/r)*exp(r*t); hold on, plot(t,pactual,'g'), hold offwhich should plot the actual solution on the same axes as your last numericl solution. What do you think?
|EXERCISE 1: Apply the Euler method to solve the logistic equation
with , and , over a time interval from 0 to 50. Try changing the number of steps (); start with and then decrease to . What do you observe? Do you have an explanation? Compare your discretization to the discrete logistic equation and think about stability.
As is illustrated in the previous exercise, it is possible for the Euler method (and, in fact, for any numerical approach) to go wrong, particularly when becomes large. In addition, the behavior of dynamics calculated using the Euler approximation generally `lag' actual system dynamics, as we will see when we compare Euler solutions to the analytic solution of the logistic equation (in the next EXERCISE). However, MATLAB has several built-in solvers for differential equations which avoid most numerical problems and are highly accurate; we will explore using these in the next sections.