doc/examples/by_class/plotter_2d/lotka_volterra.cpp File Reference

example program for class smtk::plotter_2d More...


Detailed Description

example program for class smtk::plotter_2d

Reference Tutorial Example Code
smtk::plotter_2d interactive phase space plotter
damped_SHO.cpp
damped_pendulum.cpp
lotka_volterra.cpp
multi_plot.cpp

This example interactively solves the classic Lotka-Volterra model of competition. x = population of rabbits. y = population of sheep.

#include <math.h>
#include <smtk.h>
#include <smtk/rk4.h>
#include <smtk/plotter_2d.h>

static double       x[2];    // x[0]=x, x[1]=y
static double       t;       // time

// The 2 ODEs to solve using this differential equations call-back
// function.
static void difeq(double *xdot, const double *x, double t)
{
  xdot[0] = x[0]*(3.0 - x[0] - 2*x[1]);
  xdot[1] = x[1]*(2.0 - x[0] - x[1]);
}

// Get a ODE solver object.
static smtk::rk4<double,double,double>
rk4(difeq, // differential equations call-back function
    2,     // 2 equations
    0.04   // time step
    );

// Initializing callback function.
static double *init(double x_in, double y_in, bool &do_draw)
{
  x[0] = x_in;
  x[1] = y_in;
  t = 0.0;     // time
  return x;    // return the new x,y for the plotter.
}

// Solving one point, callback function.
static double *step(bool &do_draw, bool &show_line)
{
  rk4.go(x, &t); // Go to the next time using the ODE solver.
  return x;      // return the new x,y for the plotter.
}

int main(void)
{
  smtk::plotter_2d plotter;

  plotter.
    set_window_title("Sheep VS Rabbits").
    set_callbacks(1, init, step). // pass the call-back functions
    set_line_width(3).
    set_point_width(2).
    set_scale(-0.1, 4.0, -0.1, 3.0).
    run();

  return 0;
}

Generated on Sat Aug 11 22:25:56 2007 for Simulation Toolkit (SmTk) by  doxygen 1.5.2