英文:
C language Issue simulating Lotka-Volterra model
问题
我是一名初级程序员,需要帮助来模拟 Lotka-Volterra 模型。我必须实现 Euler 和 Verlet 算法,但我不确定是否正确。在模型中有一个常数 V,我的问题是 V 在时间上不是守恒的,我不知道为什么。我也不知道结果应该是什么,所以不确定我的结果是否正确。V 起始于一个值,但随后在两个值之间振荡,并保持在那里直到模拟结束。我也不知道我是否正确实现了所要求的算法。另一个问题是我不知道如何处理变量小于 1 的情况,这似乎没有物理意义。非常感谢帮助。
以下是您的代码部分:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DT 0.0625
#define MAX 400
#define EULER
void feuler(double xn, double yn, double *xnext, double *ynext);
void fverlet(double xn, double yn, double *xnext, double *ynext);
double Cxx = 1.0;
double Cxy = 0.2;
double Cyy = 0.2;
double Cyx = 0.01;
int main()
{
// 程序的主函数部分
}
void feuler(double xn, double yn, double *xnext, double *ynext)
{
// Euler 算法的实现部分
}
void fverlet(double xn, double yn, double *xnext, double *ynext)
{
// Verlet 算法的实现部分
}
如果您需要进一步的帮助或解释,请随时提出。
英文:
Im a beginner programer and I need help with my simulation of the Lotka-Volterra model. I have to implement Euler and Verlet algorithm and I dont know if it is well done. In the model there is constant, V, my problem is that V is not conserved in time and I dont know why..I don't know what the results should be exactly, so I don't know if mine are right. V starts at one value but then oscillates between two values and remains so until the end of the simulation. Nor do I know if I have correctly implemented the algorithms that are requested. Other issue is that I dont know how to treat the cases that the variables are less than one, that apparently doesnt have physical sense.
Thank you very much for the help.
I leave here the code that I have done so far, thanks for help.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DT 0.0625
#define MAX 400
#define EULER
void feuler(double xn,double yn,double *xnext,double *ynext);
void fverlet(double xn,double yn,double *xnext,double *ynext);
double Cxx=1.0;
double Cxy=0.2;
double Cyy=0.2;
double Cyx=0.01;
int main()
{
double x;
double y;
double t;
double xaux,yaux;
double V,Vaux;
FILE *f;
x=20;
y=6;
t=0;
f=fopen("PyD.txt","w");
if (f==NULL)
{
printf("ERROR");
}
else
{
Vaux=Cyx*x-Cyy*log(x)+Cxy*y-Cxx*log(y);
fprintf(f," t\t\t x\t\t y\t\t V\t\t dx_dt\t dy_dt\n%lf\t%lf\t%lf\t%lf\t%lf\t %lf\n",t,x,y,Vaux,Cxx*x-Cxy*x*y,Cyx*x*y-Cyy*y);
#ifdef EULER
for(t=0;t<=MAX;t+=DT) // Este bucle representa el "tiempo" transcurrido.
{
feuler(x,y,&xaux,&yaux);
x=xaux;
y=yaux;
V=Cyx*x-Cyy*log(x)+Cxy*y-Cxx*log(y);
fprintf(f,"%lf\t%lf\t%lf\t%lf\n",t,x,y,V);
}
#else
for(t=0;t<MAX;t+=DT)
{
fverlet(x,y,&xaux,&yaux);
x=xaux;
y=yaux;
V=Cyx*x-Cyy*log(x)+Cxy*y-Cxx*log(y);
fprintf(f,"%lf\t%lf\t%lf\t%lf\n",t,x,y,V);
}
#endif
V=Cyx*x-Cyy*log(x)+Cxy*y-Cxx*log(y);
printf("V_i=%lf\nV_f=%lf\n",Vaux,V);
}
fclose(f);
return 0;
}
void feuler(double xn, double yn, double *xnext, double *ynext) {
double dx_dt, dy_dt;
dx_dt = Cxx * xn - Cxy * xn * yn;
dy_dt = Cyx * xn * yn - Cyy * yn;
*xnext = xn + dx_dt * DT;
*ynext = yn + dy_dt * DT;
return;
}
void fverlet(double xn, double yn, double *xnext, double *ynext) {
double dx_dt, dy_dt;
double dx_dt2, dy_dt2;
double xmed, ymed;
dx_dt = Cxx * xn - Cxy * xn * yn;
dy_dt = Cyx * xn * yn - Cyy * yn;
dx_dt2 = Cxx * dx_dt - Cxy + (dy_dt * xn + yn * dx_dt);
dy_dt2 = Cyx * (dy_dt * xn + yn * dx_dt) - Cyy * dy_dt;
*xnext = xn + dx_dt * DT + dx_dt2 * DT * DT * 0.5;
*ynext = yn + dy_dt * DT + dy_dt2 * DT * DT * 0.5;
return;
}
答案1
得分: 1
以下是要翻译的内容:
For reference, OP的振荡数据和维基链接 Lotka-Volterra方程
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论