Reputation: 101
I am trying to calculate for successive layers of a planets mass using a volume array and a uniform density. The volume starts from 0 which will give the volume between the center and the 1st shell. So I need to calculate the mass starting from there and then each layer from there. The problem I am getting into is that i need to add the calculated mass before that to get the total mass ( where the + imass[k-1] comes into play. I want to end up with total planet mass at the surface. Could I ignore that addition in the first for-loop since it wont be there?
public class MassGrids {
public double imass[];
// Initial mass with uniform density
public MassGrids ( int shells , double radius , double mass ){ // constructor for mass grids
VolGrid vg = new VolGrid ( shells, radius ); // calls volume to be used
IntlDensity rho = new IntlDensity ( mass, radius ); // calls INITIAL Density
imass = new double [ shells ] ; // Fills mass array with number of elements, shells
for ( int k = 0 ; k <= shells - 1 ; k++ ){ // For loop will define each element in array
imass[ k ] = ( vg.vol[k] * rho.irho ( mass, radius ) + imass [ k - 1 ]) ;
System.out.println(imass[k]);
}
}
Upvotes: 0
Views: 50
Reputation: 2445
for ( int k = 0 ; k <= shells - 1 ; k++ ){ // For loop will define each element in array
if(k>0)
imass[ k ] = ( vg.vol[k] * rho.irho ( mass, radius ) + imass [ k - 1 ]) ;
else
imass[ k ] = vg.vol[k] * rho.irho ( mass, radius );
System.out.println(imass[k]);
}
Yes you can ignore the addition at first loop. Because there is no mass element at -1 index location.
Upvotes: 0
Reputation: 26
why not put the first calculation outside the for loop? like this.
imass[ 0 ] = ( vg.vol[0] * rho.irho ( mass, radius )) ;
for ( int k = 1 ; k <= shells - 1 ; k++ ){ // For loop will define each element in array
imass[ k ] = ( vg.vol[k] * rho.irho ( mass, radius ) + imass [ k - 1 ]) ;
System.out.println(imass[k]);
}
Upvotes: 1