Skip to content

Instantly share code, notes, and snippets.

@Corwinpro
Created November 1, 2013 04:17
Show Gist options
  • Save Corwinpro/7260896 to your computer and use it in GitHub Desktop.
Save Corwinpro/7260896 to your computer and use it in GitHub Desktop.
Victim-predator
#define _CRT_SECURE_NO_DEPRECATE
#include "stdio.h"
#include "math.h"
#include "conio.h"
#include <iostream>
int a = 10;
int b = 2;
int c = 2;
int d = 10;
double U(double t,double x,double y)
{
return a*x - b*x*y;
}
double V(double t,double x,double y)
{
return c*x*y - d*y;
}
void solve()
{
double t = 0;
double step = 0.001;
double x = 4.99;
double y = 5.01;
double q0, q1, q2, q3;
double k0, k1, k2, k3;
FILE * file;
file = fopen("file.txt", "w");
while (t < 10.0)
{
fprintf(file, "%.8e %.8e %.8e\n", t, x , y);
q0 = U(t, x, y);
k0 = V(t, x, y);
q1 = U(t+step/2.0,x+k0*step/2.0, y+q0*step/2.0);
k1 = V(t+step/2.0,x+k0*step/2.0, y+q0*step/2.0);
q2 = U(t+step/2.0,x+k1*step/2.0, y+q1*step/2.0);
k2 = V(t+step/2.0,x+k1*step/2.0, y+q1*step/2.0);
q3 = U(t+step,x+k2*step, y+q2*step);
k3 = V(t+step,x+k2*step, y+q2*step);
x = x + step*(k0+2.0*k1+2.0*k2+k3)/6.0;
y = y + step*(q0+2.0*q1+2.0*q2+k3)/6.0;
t = t + step;
}
fclose(file);
}
void main()
{
solve();
getchar();
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment