首先,我很抱歉在我的代码中可能存在废话,我更像是一个初学者..我正在尝试使用
编译此代码#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//initial conditions
//#define rs0 2.064594102e11
//#define re0 5.064594102e16
//#define vs0 2.929599982
//#define ve0 29295.99982
#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
typedef void (*vector_function)(double* y, double t, double* f);
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];
//diff[i]=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;
}
for (int i=0; i<12; ++i)
{
y[i] = yn[i];
}
}
void F(double *y, double t, double* f)
{
double re0 = (a*(1+e)*ms)/(me+ms);
double rs0 = (-me*re0)/ms;
double kE = (G*ms*me/a)*(-0.5 + (1/(1+e)));
double ve0 = sqrt((2*kE)/(me*(1+(me/ms))));
double vs0 = -me*ve0/ms;
double Tyear = (2*3.141592*pow(a, 2/3))/sqrt(G*(ms+me));
double d[3] = {y[0]-y[6], y[1]-y[7], y[5]-y[8]};
double dcube = pow((y[0]-y[6])*(y[0]-y[6])+(y[1]-y[7])*(y[1]-y[7])+(y[5]-y[8])* (y[5]-y[8]), 3);
for (int i = 0; i<3; ++i)
{
//Force Earth
f[i] = G*ms*d[i]/dcube;
//Velocity Earth
f[3+i] = y[3+i];
//Force Sun
f[6+i] = -G*me*d[i]/dcube;
//Velocity Sun
f[9+i] = y[9+i];
}
int main(int argc, char ** argv)
{
int Tmax, m;
//printf("Please input number of the years\n");
//scanf("%i",&y);
Tmax = Tyear;
printf("please input the number of steps \n");
scanf("%d",&m);
// y[11] = {re0, 0, 0, 0, ve0, 0, rs0, 0, 0, 0, vs0, 0};
double y[11], yn[11];
//,sum[2];
y[0]=re0;
y[1]=0;
y[2]=0;
y[3]=0;
y[4]=ve0;
y[5]=0;
y[6]=rs0;
y[7]=0;
y[8]=0;
y[9]=0;
y[10]=vs0;
y[11]=0;
double t=0;
double dt=( Tmax-t )/m;
// the runge-kuta integrator starts from here
for (int k=0; k<m;k++)
{
t=t+dt;
integrator(F, 12, y, t, dt, yn);
printf("%e %e %e %e\n", t, y[0], y[1], y[2]);
for(int i=0; i<3; ++i) y[i] = yn[i];
}
printf("the intigrated components of the radius-vector and the velosity of the Earth are:\n");
for (int i=0; i<6; ++i)
{
printf("%lf", yn[i]);
}
printf("the intigrated components of the radius-vector and the velosity of the Sun are:\n");
for (int i=6; i<12; ++i)
{
printf("%lf", yn[i]);
}
/* printf("the intigrated vr is %lf\n",yn[1]);
printf("the intigrated theta is %lf\n",yn[2]);*/
//fprintf(f,"%lf/t%lf/n",y[0],y[2]);
//fclose(f);
return 0;
}
}
我编译它:
Main: main.c
gcc -c main.c -o main -std=c99 -lm
它没有创建执行文件,我获得了许可被拒绝。
A也尝试使用相同的makefiel而没有-c ..不起作用并产生这一主题中陈述的错误。帮助=(
答案 0 :(得分:1)
所以,至少有两个问题:
F()
函数缺少结束括号。-c
。答案 1 :(得分:1)
C中不允许使用嵌套函数,即无法在另一个函数内定义函数。您正在函数
中定义main
void F(double *y, double t, double* f){..}
这是错误的。