// This does the 3 body 3-4-5 configuration. // This version is the simplest. // This version updates x too often. #include #include static double s(double x){return x*x;} typedef struct {double x, y, xd, yd, m, mid;} pt; // xd & yd include factor dt. mid is dt^2/mass. int main(){ pt z[] = {{0,4,0,0,3,0},{3,0,0,0,4,0},{0,0,0,0,5,0}}; double dt = .000001; double t = 0; void pr(char * m){int k; printf("t=%12.7f %s\n", t, m); for(k=0; k<3; ++k) {pt*p = z+k; printf("%10.7f %10.7f %10.7f %10.7f\n", p->x, p->y, p->xd/dt, p->yd/dt);} } void conf(pt* j, pt* k){ double dx = j->x - k->x, dy = j->y - k->y, s2 = s(dx) + s(dy); double d = j->m*k->m/(sqrt(s2)*s2); double fx = dx*d, fy = dy*d; j->xd -= fx*j->mid; j->yd -= fy*j->mid; k->xd += fx*k->mid; k->yd += fy*k->mid; j->x += j->xd; j->y += j->yd; k->x += k->xd; k->y += k->yd; } void step(){conf(z,z+1); conf(z,z+2); conf(z+1,z+2); t += dt;} {int j=3; while(j--) z[j].mid = s(dt)/z[j].m;} pr("init"); step(); pr("once"); while(1){int j = 9999999; while(j--) step(); pr("Chug");} }