C语言问题:模拟Lotka-Volterra模型

huangapple go评论53阅读模式
英文:

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 &lt;stdio.h&gt;
    #include &lt;stdlib.h&gt;
    #include &lt;math.h&gt;
    
    #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(&quot;PyD.txt&quot;,&quot;w&quot;);
        if (f==NULL)
        {
            printf(&quot;ERROR&quot;);
        }
        else 
        {
            Vaux=Cyx*x-Cyy*log(x)+Cxy*y-Cxx*log(y);
            fprintf(f,&quot;    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&quot;,t,x,y,Vaux,Cxx*x-Cxy*x*y,Cyx*x*y-Cyy*y);
    
    #ifdef EULER
            for(t=0;t&lt;=MAX;t+=DT) // Este bucle representa el &quot;tiempo&quot; transcurrido.
            {
                feuler(x,y,&amp;xaux,&amp;yaux);
    
                x=xaux;
                y=yaux;
    
                V=Cyx*x-Cyy*log(x)+Cxy*y-Cxx*log(y);
    
                fprintf(f,&quot;%lf\t%lf\t%lf\t%lf\n&quot;,t,x,y,V);
    
            }
    #else
            for(t=0;t&lt;MAX;t+=DT)
            {
                fverlet(x,y,&amp;xaux,&amp;yaux);
    
                x=xaux;
                y=yaux;
    
                V=Cyx*x-Cyy*log(x)+Cxy*y-Cxx*log(y);
    
                fprintf(f,&quot;%lf\t%lf\t%lf\t%lf\n&quot;,t,x,y,V);
            }
    #endif
            V=Cyx*x-Cyy*log(x)+Cxy*y-Cxx*log(y);
    
            printf(&quot;V_i=%lf\nV_f=%lf\n&quot;,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方程

C语言问题:模拟Lotka-Volterra模型

英文:

For reference, OP's oscillating data and wiki link Lotka–Volterra equations

C语言问题:模拟Lotka-Volterra模型

huangapple
  • 本文由 发表于 2023年6月29日 04:58:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576665.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定