{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

practical08_sol - Practical Session 8 Solutions Exercises...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Practical Session 8 — Solutions Exercises 1. (a) The code is below. The results are in Figure 1. The populations head towards a steady state where they oscillate, like the original Lotka-Volterra equations. However the phase space diagram shows that the final oscillating state is approached from the starting values as a limit (this is an example of a ‘limit cycle’). /* pred_prey_mod2.c * Code to solve modified Lotka-Volterra ODEs using second order * Runge-Kutta. The ODEs have been modified to incorporate logistic growth * of the prey in the absence of predators, the finite predation of * individual predators, and logistic growth of the predators to a carrying * capacity proportional to the prey numbers. */ #include <stdio.h> #define N 2 /* number of ODEs */ #define TMIN 0.0 /* starting point for integration */ #define TMAX 15.0 /* stopping point for integration */ #define NSTEP 300 /* number of integration steps */ #define A 5.0 /* growth parameter for prey */ #define B 10.0 /* predation versus growth factor for predators */ #define C 0.5 /* predation factor */ #define N0 0.5 /* initial value */ #define P0 0.5 /* initial value */ void rk2(float xin, float yin[], float yout[], float h); void derivs(float xin, float yin[], float dydx[]); int main() { int i; float h = (TMAX - TMIN)/(1.0*NSTEP); /* stepsize for integration */ float xin, yin[N], yout[N]; float t[NSTEP+1], prey[NSTEP+1], pred[NSTEP+1]; /* Define array of t values */ for (i = 0; i <= NSTEP; i++) t[i] = TMIN + h*i; /* starting values */ prey[0] = N0; pred[0] = P0; /* do the integration */ printf("%f %f %f\n", t[0], prey[0], pred[0]); for (i = 0; i <= NSTEP-1; i++) { xin = t[i]; yin[0] = prey[i]; yin[1] = pred[i]; rk2(xin, yin, yout, h); /* do one integration step */ 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
prey[i+1] = yout[0]; pred[i+1] = yout[1]; printf("%f %f %f\n", t[i+1], prey[i+1], pred[i+1]); } return 0; } void derivs(float xin, float yin[], float dydx[]) { /* right hand sides of ODEs */ dydx[0] = (A*(1 - yin[0]) - B*yin[1]/(yin[0] + C))*yin[0]; dydx[1]= (1 - yin[1]/yin[0])*yin[1]; return; } void rk2(float xin, float yin[], float yout[], float h) { /* Second order Runge-Kutta scheme */ int i; float k1[N], k2[N], yt[N], dydx[N]; /* N is the number of ODEs */ /* Evaluate k1 */ derivs(xin, yin, dydx); for (i = 0; i < N; i++) { k1[i] = h*dydx[i]; yt[i] = yin[i] + 0.5*k1[i]; } /* Evaluate k2, then update the dependent variable */ derivs(xin + 0.5*h, yt, dydx); for (i = 0; i < N; i++) { k2[i] = h*dydx[i]; yout[i] = yin[i] + k2[i]; } return; } (b) The results are shown in Figure 2. Both numerical solutions head towards the same limit
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}