/* ************************************************************************ * ルンゲ・クッタ法による2次元運動方程式の解法 * * 速度に比例する抵抗がある斜方投射 * ************************************************************************ t:時間、x[0]=x、x[1]=y:位置、x[2]=vx、x[3]=vy:速度、dt:時間の刻み幅 t0、x0、y0、vx0、vy0:初期値 v0:初速度、theta:投げ上げ角度 alpha:速度に比例する抵抗の係数 */ #include #include #define N 4 /* 方程式の数 */ double alpha; main() { void rungen(double t, double x[], double dt); double f(double t, double x[], int i); double t, x[N], v0, vx0, vy0, theta, dt, h; double pi; int j; /* 出力ファイルの指定 */ FILE *output; output = fopen("xy.dat","w"); /* xy.datにデータをセーブ */ /* 初速度 */ printf("Input: v0\n"); scanf("%lf", &v0); printf("v0=%g\n", v0); /* 投げ上げ角度 */ printf("Input: theta\n"); scanf("%lf", &theta); printf("theta=%g\n", theta); pi=3.1415926535897932385; vx0=v0*cos(pi*theta/180); vy0=v0*sin(pi*theta/180); /* 速度に比例する抵抗の係数 */ printf("Input: alpha\n"); scanf("%lf", &alpha); printf("alpha=%g\n", alpha); /* 時間の刻み幅 */ printf("Input: dt\n"); scanf("%lf", &dt); printf("dt=%g\n", dt); /* 初期値 */ t = 0.0; x[0] = 0.0; /* 初期位置 */ x[1] = 0.0; /* 初期位置 */ x[2] = vx0; /* 初期速度 */ x[3] = vy0; /* 初期速度 */ fprintf(output, "%f\t%f\t%f\n", t, x[0], x[1]); while(x[1] >= 0.0) { /* ルンゲ・クッタ法ルーチンを呼び出す */ rungen(t, x, dt); t += dt; fprintf(output, "%f\t%f\t%f\n", t, x[0], x[1]); } printf("data stored in xy.dat\n"); fclose(output); } /*-----------------------end of main program--------------------------*/ /* Runge-Kutta法 */ void rungen(double t, double x[], double dt) { double f(double t, double x[], int i); double h=dt/2.0, t1[N], t2[N], t3[N], /* Runge-Kutta法のための */ k1[N], k2[N], k3[N],k4[N]; /* 一時変数 */ int i; for (i=0; i