代码产生nan

时间:2013-12-06 22:16:35

标签: c

我已经发布了类似的代码,但现在它产生了nan =(可能是我以错误的方式声明了角度。这是一个集成商:

#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include "rk4.h"

#define M_PI 3.141592653589793


void integrator(vector_function f, int n, double* y,
            double t, double dt, double* yn)
{
double k1[n],k2[n],k3[n],k4[n],ytmp[n];
double deriv[n]; // note that dynamic arrays are
// a C99 feature -- check your compiler flags
f(y,t,deriv);
for(int i=0;i<n;++i)
{
    k1[i]=deriv[i]*dt;
    ytmp[i]=y[i]+0.5*dt*deriv[i];
}
f(ytmp,t+0.5*dt,deriv);
for(int i=0;i<n;++i)
{
    k2[i]=dt*deriv[i];
    ytmp[i]=y[i]+0.5*dt*deriv[i];
}
f(ytmp,t+0.5*dt,deriv);
for(int i=0;i<n;++i)
{
    k3[i]=dt*deriv[i];
    ytmp[i]=y[i]+dt*deriv[i];
}
f(ytmp,t+dt,deriv);
for(int i=0;i<n;++i) k4[i]=dt*deriv[i];

for(int i=0;i<n;++i)
{
    yn[i]=y[i]+(k1[i]+2*k2[i]+2*k3[i]+k4[i])/6;
}
}

这是一个功能:

#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "rk4.h"

//initial conditions

#define M_PI 3.141592653589793
#define ms 1.9891e30
#define me 5.9736e24
#define a 149598261e3
#define e 0.0167112303531389
#define G 6.67428e-11
#define Tday 86164.1
#define Tyear 31552844.28
#define I1 8.008e37
#define I3 8.034e37
#define Theta0 24.45*2*M_PI/360.0


void F(double* y, double t, double* f);

int main(int argc, char ** argv)
{
    double tf;
    int m;
    printf("Please input the number of years\n");
    scanf("%lf",&tf);
    tf = tf*Tyear;
    printf("please input the number of steps \n");
    scanf("%d",&m);
    double yn[18];
    double t=0;
    double dt=(tf-t) / m;

    double re0 = a*(1+e)*ms/(me+ms);
    double KE  = G*ms*me*(1.0/(1+e)-0.5)/a;
    double ve0 = sqrt(2*KE/(me*(1+me/ms)));

double rs0 = -me*re0/ms;
double vs0 = -me*ve0/ms;

    //angles
    double vpsy0 = (2.0*M_PI)/Tday;
    double y[18]= {re0, 0, 0, 0, ve0, 0, rs0, 0, 0, 0, vs0, 0, 0, Theta0, 0, 0, 0, vpsy0};


    //integrator


    for (int k=0; k<m;k++)
    {
    integrator(F, 18,  y, t, dt, yn);
        t=t+dt;
        for(int i=0; i<18; ++i) y[i] = yn[i];
        printf("%e ", t); for(int i=0; i<18; ++i) printf("%e ", y[i]);
        printf("\n");
    }
 //        printf("the integrated components re are %e %e %e\n",yn[0], yn[1], yn[2]);
 //        printf("the integrated components ve are %e %e %e\n",yn[3], yn[4], yn[5]);
 //        printf("the integrated components rs are %e %e %e\n",yn[6], yn[7], yn[8]);
 //        printf("the integrated components vs are %e %e %e\n",yn[9], yn[10], yn[11]);
   // printf("the intigrated theta is %lf\n",yn[2]);
    //printf("%lf\n",dcube);
    //fprintf(f,"%lf/t%lf/n",y[0],y[2]);
    //fclose(f);
    return 0;

}


void F(double* y, double t, double* f)
{
double d[3] = {y[0]-y[6], y[1]-y[7], y[5]-y[8]};
double dcube;
double dsqrt;
double d5;
double d3 = d[0]*sin(y[13])*sin(y[14])-d[1]*cos(y[13])*sin(y[14])+d[2]*cos(y[14]);
double c0;

dsqrt = d[0]*d[0] + d[1]*d[1] + d[2]*d[2];
dcube = pow(dsqrt, 1.5);
d5 = pow(dsqrt, 2.5);
c0 = 2.0*M_PI/Tday;

//the term angular[] (different for every component)

double a1 = sin(y[12])*sin(y[13]);
double a2 = -cos(y[12])*sin(y[13]);
double a3 = cos(y[13]);

double angular[3]={a1, a2, a3};

for (int i = 0; i<3; ++i)
{

  //velocity Earth
  f[i] = y[3+i];

  //acceleration Earth
  f[3+i] = -(G*ms*d[i]/dcube) + (3.0*G*ms*(I1-I3)*((d[i]/2.0) - (5.0*pow(d3, 2)*d[i]/2.0*dsqrt) + (d3*angular[i]/2.0)))/d5*me;

  //Velocity Sun
  f[6+i] = y[9+i];

  //acceleration Sun
  f[9+i] = (G*me*d[i]/dcube) - (3.0*G*(I1-I3)*((d[i]/2.0) - (5.0*pow(d3, 2)*d[i]/2.0*dsqrt) + (d3*angular[i]/2.0)))/d5;



  }

  //angles
  f[12] = y[15];
  f[13] = y[16];
  f[14] = y[17];
  f[15] = (2.0*y[15]*y[16]*cos(y[13]) + I3*c0*y[16]/I1 + 3.0*G*ms*(I1-I3)*d3*(y[0]*cos(y[12])+y[1]*sin(y[12])))/sin(y[13]);
  f[16] = pow(y[15], 2)*sin(y[13])*cos(y[13]) - (I3*c0*y[15]*sin(y[13])/I1) + 3.0*G*ms*(I1-I3)*d3*((d[0]*sin(y[12])-d[1]*cos(y[12]))*cos(y[13])-d[2]*sin(y[13]))/(d5*I1);
  f[17] = y[15]*y[16]*sin(y[13]) - f[16]*cos(y[13]);
 }

这是一个Makefile:

integrator: integrator.o rk4.o
        gcc -o $@ $^ -lm

integrator.o: integrator.c
        gcc -g -c integrator.c  -std=c99 -lm
rk4.o: rk4.c
        gcc -g -c rk4.c  -std=c99 -lm
clean:
        rm -rf *.o integrator

请帮忙!我仍然有可能弄乱了公式,但我认为还有错误的声明或某些溢出......

提前谢谢大家!

1 个答案:

答案 0 :(得分:3)

考虑到你的数学,这可能就是你最终得到NaN的方式。

Step 1:  something / 0 -> INF (or -INF)
Step 2:  something * INF -> NaN

因此,在代码中为每个除数放置测试以查看它是否为或接近于零:我通常会测试该值为(-1e-20 < x ) && ( x < 1e-20 )

您也可以使用<math.h> isinf()isnan()中的测试来测试。