Fitzhugh-Nagumo模型的这个C实现有什么错误?

时间:2015-08-30 08:40:07

标签: c matlab differential-equations

我已经为Fitzhugh-Nagumo模型实现了MATLAB代码并获得了图表,但是当我将它转换为如下所示的C代码时,它没有给我正确的输出。特别是神经元没有尖峰。任何人都可以指出错误吗?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
double rand2(void);
int main()
{
    int n,i;
    double dt,r[120000],g[120000],rr ;// 20min = 20*60/dt = 120000
    double a=0.08,b=0.7,c=0.8,gg;
    //FILE * temp = fopen("fhn.dat", "w");
    srand(time(NULL));
    //T=200*60;
    dt=0.01;
    r[0]=-0.212002413260425;//initial values
    g[0]=-0.869601930608358;

    for(n=1;n<=120000;n++)
    {
    r[n]=(dt)*((a*g[n-1])+b-(c*r[n-1]))+r[n-1];
    rr=rand2(); //uniform random number between 0 and 1
    gg=pow(g[n-1],3); // g[n-1]^3
    g[n]=(dt)*(g[n-1]-gg-r[n-1]+rr)+g[n-1];
    //printf("rr %f\n",rr);
    }

    FILE *gnuplot = popen("gnuplot -persistent", "w");
    fprintf(gnuplot, "set title \"Fitzhugh-Nagumo\" \n");
    fprintf(gnuplot, "plot '-' with lines \n");
    for (i=0;i<=120000-1;i++)
    {
        fprintf(gnuplot, "%g %g\n", r[i],g[i]);
        //fprintf(temp, "%lf %lf \n",r[i], g[i]); //Write the data to a temporary file

    }
    fprintf(gnuplot, "e\n");
    fflush(gnuplot);
    pclose(gnuplot);
    //fclose(temp);
    return 0;
}

double rand2()
{
    return (double)rand() / (double)RAND_MAX ;
}

注意:我希望输出如下图:

enter image description here

更新:链接到MATLAB Code

但我得到的是:

enter image description here

1 个答案:

答案 0 :(得分:1)

\A(.*\r?\n){3}|(\r?\n.*){3}\z 被错误地加上括号。取代

r[n]

r[n]=(dt)*((a*g[n-1])+b-(c*r[n-1]))+r[n-1];

,结果看起来像是预期的。