Using nested loops
我仍在尝试实现上述问题中所述的相同内容。基本上我的代码现在看起来像这样:
for(int ac = 0; ac < gravityObject.length; ac++){
gravField[ac].setVector(0,0);
System.out.println(ac);
for(int ad=0;ad<gravityObject.length;ad++){
System.out.println(ad);
if(ac!=ad){
gravField[ac].increaseBy(gravityObject[ac].aDueToGravity(planetMass[ad], position[ad], position[ac]));
}
else{
//do nothing
}
gravField[ac].print();
}
}
这很烦人,我不明白为什么会这样!
例:
我想计算由于太阳和月亮引起的地球加速度。所以我有double [] planetMass = {earthMass,sunMass,moonMass},一组粒子对象Particle [] planets = {earth,sun,moon}以及它们经历的重力引力的数组,它们尚待计算:
PhysicsVector [] gravField = {earthGrav,sunGrav,moonGrav}。
我的循环旨在填充此数组。我想对sunGrav和moonGrav求和,以求出地球的加速度,并将结果存储在earthGrav中,因此存储在gravField [0]中。然后是EarthGrav + moonGrav,这应该存储为gravField [1]。
我的循环正确计算了gravField [0],gravField [1]和gravField [2]的值,然后不幸地将所有gravField []设置为等于gravField [2]。
据推测这是因为我一直将gravField [ac]重置为零。但是,如果我不这样做,那么我就不会获得正确的值。我主要的课程的其余部分(问题绝对是循环):
公共静态PhysicsVector [] copyArray(PhysicsVector [] a){
int length = a.length;
PhysicsVector []复制=新的PhysicsVector [length];
System.arraycopy(a,0,复制,0,长度);
返回副本;
}
public static double sumArray(double[] p){
double sum = 0;
for(int z= 0; z < p.length; z++){
sum += p[z];
}
return sum;
}
public static PhysicsVector[] subtract(PhysicsVector[] diff, PhysicsVector g){
for (int ab=0; ab<diff.length;ab++){
diff[ab].decreaseBy(g);
}
return diff;
}
public static void main(String[] args) throws IOException{
java.io.File file = new java.io.File("output.txt" );
java.io.PrintWriter n = new PrintWriter(file);
//Initialise variables here
PhysicsVector earthInitialPos = new PhysicsVector();
PhysicsVector earthInitialV = new PhysicsVector();
PhysicsVector sunInitialV = new PhysicsVector();
PhysicsVector sunInitialPos = new PhysicsVector();
PhysicsVector moonInitialPos = new PhysicsVector();
PhysicsVector moonInitialV=new PhysicsVector();
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the size of the time step:");
double timeStep = scanner.nextDouble();
//SET PLANETS' INITIAL POSITIONS
//Set Earth's initial position and initial velocity
earthInitialPos.setVector(1.4960*Math.pow(10,11),0);
earthInitialV.setVector(0,29785.5889);
//Set the sun's initial position
sunInitialPos.setVector(0,0);
sunInitialV.setVector(0,-0.08945); //v = 2*pi*R/T
//Set the moon's initial position and velocity
moonInitialPos.setVector(3.844*Math.pow(10,8),0);
moonInitialPos.increaseBy(earthInitialPos);
moonInitialV.setVector(0,1023.2195);
moonInitialV.increaseBy(earthInitialV);
//CREATE GRAVFIELD OBJECTS
GravField sunGravField = new GravField(sunMass, sunRadius, sunInitialPos);
GravField earthGravField = new GravField(earthMass, earthRadius, earthInitialPos);
GravField moonGravField = new GravField(moonMass, moonRadius, moonInitialPos);
//CREATE PARTICLE OBJECTS
Particle earth = new Particle(earthMass, earthInitialPos, earthInitialV);
Particle sun = new Particle(sunMass, sunInitialPos, sunInitialV);
Particle moon = new Particle(moonMass, moonInitialPos, moonInitialV);
double time = 0;
double finalTime = 31557600; //Run for one earth year
//Initialise newV and set equal to initialV
PhysicsVector newV = new PhysicsVector();
newV = earthInitialV;
PhysicsVector sunNewV = new PhysicsVector();
sunNewV = sunInitialV;
PhysicsVector moonNewV = new PhysicsVector();
moonNewV = moonInitialV;
PhysicsVector newSunGrav = new PhysicsVector();
PhysicsVector newEarthGrav = new PhysicsVector();
PhysicsVector newMoonGrav = new PhysicsVector();
GravField[] gravityObject = {earthGravField, sunGravField, moonGravField};
PhysicsVector[] position = {earthInitialPos, sunInitialPos, moonInitialPos};
PhysicsVector[] velocity = {newV, sunNewV, moonNewV};
PhysicsVector[] gravField = {zero, zero, zero};
double[] planetMass = {earthMass, sunMass, moonMass};
Particle[] planets = {earth, sun, moon};
PhysicsVector a = new PhysicsVector(0,0);
//Calculate the centre of mass and subtract position from positions of planets, so c.o.m is at origin
PhysicsVector centreOfMass = new PhysicsVector();
centreOfMass = earth.centreOfMass(planetMass, position);
position = SolarSim.subtract(position, centreOfMass);
//Calculate centre of mass velocity and subtract from planet velocities
PhysicsVector centreOfMassVelocity = new PhysicsVector();
centreOfMassVelocity = earth.cOMVel(planetMass, velocity);
velocity = SolarSim.subtract(velocity, centreOfMassVelocity);
//Calculate fields of planets
for(int ac=0; ac<gravityObject.length; ac++){
gravField[ac].setVector(0,0);
System.out.println(ac);
for(int ad=0;ad<gravityObject.length;ad++){
System.out.println(ad);
if(ac!=ad){
//gravField[ac].increaseBy(gravityObject[ac].aDueToGravity(planetMass[ad], position[ad], position[ac]));
a.increaseBy(gravityObject[ac].aDueToGravity(planetMass[ad], position[ad], position[ac]));
gravField[ac] = a;
}
else{
//do nothing
}
gravField[ac].print();
}
}
System.out.println(Arrays.toString(gravField));
PhysicsVector[] newP = new PhysicsVector[position.length];
PhysicsVector[] newGrav = {zero,zero,zero};
PhysicsVector[] newVel = new PhysicsVector[velocity.length];
PhysicsVector sum1 = new PhysicsVector(0,0);
PhysicsVector sum2 = new PhysicsVector(0,0);
PhysicsVector sum3 = new PhysicsVector(0,0);
//do{
PhysicsVector[] y = new PhysicsVector[gravField.length];
y=copyArray(gravField);
PhysicsVector[] z = new PhysicsVector[gravField.length];
z=copyArray(gravField);
for(int i=0; i<planets.length;i++){
sum1.setVector(0,0);
for(int s=0;s<z.length;s++){
if(i!=s){
sum1.increaseBy(z[s]);
}
else{
sum1.scale(1);
}
}
newP[i] = planets[i].updatePosition(position[i], velocity[i], timeStep, sum1);
}
newP[2].increaseBy(newP[0]);
//Calculate the centre of mass and subtract position from positions of planets, so c.o.m is at origin
centreOfMass = earth.centreOfMass(planetMass, newP);
newP = SolarSim.subtract(newP, centreOfMass);
for(int j=0; j<gravityObject.length; j++){
for(int l=0;l<gravityObject.length;l++){
if(j!=l){
newGrav[j].increaseBy(gravityObject[j].aDueToGravity(planetMass[j], position[j], position[l]));
}
else{
}
}
}
for(int k=0; k<planets.length; k++){
sum2.setVector(0,0);
sum3.setVector(0,0);
for(int m = 0; m<y.length;m++){
if(k!=m){
sum2.increaseBy(y[m]);
sum3.increaseBy(newGrav[m]);
}
else{
sum2.scale(1);
sum3.scale(1);
}
}
newVel[k] = planets[k].updateVelocity(velocity[k], timeStep, sum2, sum3);
}
newVel[2].increaseBy(newVel[0]);
//Calculate centre of mass velocity and subtract from planet velocities
centreOfMassVelocity = earth.cOMVel(planetMass, newVel);
newVel = SolarSim.subtract(newVel, centreOfMassVelocity);
gravField = copyArray(newGrav);
time+=timeStep;
//n.println(e+" "+z);
//n.println(g+" "+h);
//}while (time<=finalTime);
System.out.println(Arrays.toString(newP));
n.close();
}
}
最佳答案
尝试在单独的步骤中将gravField清零:
for(int ac = 0; ac < gravityObject.length; ac++){
gravField[ac].setVector(0,0);
}
for(int ac = 0; ac < gravityObject.length; ac++){
System.out.println(ac);
/// the rest of your code as above, without setVector(0,0)