prey_pred_mod_1

# prey_pred_mod_1 - printf"%f%f%f\n" t[0 prey[0...

This preview shows pages 1–2. Sign up to view the full content.

/* pred_prey_mod1.c * Example code to solve the modified Lotka-Volterra ODEs using second * order Runge-Kutta. */ #include <stdio.h> #define N 2 /* number of ODEs */ #define TMIN 0.0 /* starting point for integration */ #define TMAX 30.0 /* stopping point for integration */ #define NSTEP 200 /* number of integration steps */ #define A 0.5 /* parameter in non-dim. Lotka-Volterra equations */ #define DELTA 0.2 /* small parameter in modified equations */ #define N0 0.5 #define P0 0.5 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]; /* normalised time coordinate */ float prey[NSTEP+1], pred[NSTEP+1]; /* predator, prey populations */ /* 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 */

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 */ 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] = ((1 - DELTA*yin[0]) - yin[1])*yin[0]; dydx[1] = -A*(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; }...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

prey_pred_mod_1 - printf"%f%f%f\n" t[0 prey[0...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online