# include # include # include using namespace std; void f4 ( double t, double u[], double dudt[] ); int main ( ) // // EULER_F4 uses the Euler method on problem 4, the predator prey problem, // which includes 2 variables whose equations are coupled. // { double dt = 0.01, dudt[2], t0 = 0.0, t1, tmax = 20.0, u0[2] = { 300, 150 }, u1[2]; int i; while ( true ) { cout << " " << t0 << " " << u0[0] << " " << u0[1] << "\n"; if ( tmax <= t0 ) { break; } // // Get the derivative at the current solution. // f4 ( t0, u0, dudt ); // // Update time to T1, and compute U1. // t1 = t0 + dt; for ( i = 0; i < 2; i++ ) { u1[i] = u0[i] + dt * dudt[i]; } // // Shift the data. // t0 = t1; for ( i = 0; i < 2; i++ ) { u0[i] = u1[i]; } } return 0; } void f4 ( double t, double u[], double dudt[] ) { double alpha = 0.01, f, r; r = u[0]; f = u[1]; dudt[0] = 2 * r - alpha * r * f; dudt[1] = - f + alpha * r * f; return; }