What the Hell?
The unthinkable has happened. I've posted on my blog
(well, I did warn you in the description)
Anyway, I need to talk about programming, and this seems the best place.
Ok, I'm trying to simulate the effects of gravity on an object in a C program.
This is it so far:
#include
#include
#define GRAVITY 6.67 /* This needs to be changed to 6.67e-11 */
main()
{
long sunmass, planetmass, cycles;
float pX, pY, fX, fY, vX, vY, f, n;
/* Set starting Conditions */
planetmass = 1;
sunmass = 10;
vX = 0;
vY = 0;
printf("Initial X co-ordinate\n");
scanf("%f", &pX);
printf("Initial Y co-ordinate\n");
scanf("%f", &pY);
printf("Number of seconds\n");
scanf("%d", &cycles);
printf("%4.2f, %4.2f, %d\n", pX, pY, cycles);
/* Main process */
while ((cycles > 0) && (pX != 0) && (pY != 0)) /* Last two statements are required to avoid dividing by 0 */
{
n = (pX * pX)/(pY * pY);
f = (GRAVITY * sunmass * planetmass)/((pX*pX) + (pY*pY));
fX = sqrt(n * (f * f / (n + 1)));
fY = sqrt(f * f / (n + 1));
if (pX > 0) /* These 8 lines allow for negative values of pX and pY */
vX = vX - (fX / planetmass);
else vX = vX + (fX / planetmass);
if (pY > 0) vY = vY - (fY / planetmass);
else vY = vY + (fY / planetmass);
pX = pX + vX;
pY = pY + vY;
printf("x=%4.2f, y=%4.2f vX=%4.2f, vY=%4.2f, fX=%4.2f, fY=%4.2f, f=%4.2f, t=%d\n", pX, pY, vX, vY, fX, fY, f, cycles);
if ( (pX <> -1)) /* These 4 lines prevent a bug where after passing (0,0), the planet accelerates rapidly. */
pX = 0;
if ((pY <> -1))
pY = 0;
--cycles;
}
printf("End of Simulation\n");
}
Apologies for the messiness, it didn't copy across properly.
Pretty impressive for learning to write in C two days ago, hmm?
The problem I'm faced with now is that gravity is only calculated every second in the simulation. This has led to some blatant physics errors, such as violation of the conservation of energy principle etc.
I could just calculate it more often, but that won't solve the basic problem. I suspect that this is why Newton invented fluxions (calculus to the rest of us). So I nowhave to learn that too. Wonderful. Oh well, it was in my New Years resolution...
(well, I did warn you in the description)
Anyway, I need to talk about programming, and this seems the best place.
Ok, I'm trying to simulate the effects of gravity on an object in a C program.
This is it so far:
#include
#include
#define GRAVITY 6.67 /* This needs to be changed to 6.67e-11 */
main()
{
long sunmass, planetmass, cycles;
float pX, pY, fX, fY, vX, vY, f, n;
/* Set starting Conditions */
planetmass = 1;
sunmass = 10;
vX = 0;
vY = 0;
printf("Initial X co-ordinate\n");
scanf("%f", &pX);
printf("Initial Y co-ordinate\n");
scanf("%f", &pY);
printf("Number of seconds\n");
scanf("%d", &cycles);
printf("%4.2f, %4.2f, %d\n", pX, pY, cycles);
/* Main process */
while ((cycles > 0) && (pX != 0) && (pY != 0)) /* Last two statements are required to avoid dividing by 0 */
{
n = (pX * pX)/(pY * pY);
f = (GRAVITY * sunmass * planetmass)/((pX*pX) + (pY*pY));
fX = sqrt(n * (f * f / (n + 1)));
fY = sqrt(f * f / (n + 1));
if (pX > 0) /* These 8 lines allow for negative values of pX and pY */
vX = vX - (fX / planetmass);
else vX = vX + (fX / planetmass);
if (pY > 0) vY = vY - (fY / planetmass);
else vY = vY + (fY / planetmass);
pX = pX + vX;
pY = pY + vY;
printf("x=%4.2f, y=%4.2f vX=%4.2f, vY=%4.2f, fX=%4.2f, fY=%4.2f, f=%4.2f, t=%d\n", pX, pY, vX, vY, fX, fY, f, cycles);
if ( (pX <> -1)) /* These 4 lines prevent a bug where after passing (0,0), the planet accelerates rapidly. */
pX = 0;
if ((pY <> -1))
pY = 0;
--cycles;
}
printf("End of Simulation\n");
}
Apologies for the messiness, it didn't copy across properly.
Pretty impressive for learning to write in C two days ago, hmm?
The problem I'm faced with now is that gravity is only calculated every second in the simulation. This has led to some blatant physics errors, such as violation of the conservation of energy principle etc.
I could just calculate it more often, but that won't solve the basic problem. I suspect that this is why Newton invented fluxions (calculus to the rest of us). So I nowhave to learn that too. Wonderful. Oh well, it was in my New Years resolution...

1 Comments:
I did not understand that at all, youll have to explain it to me sometime, and at least you kept your new years resolution. Good post though, not as depressing as the others.
Post a Comment
<< Home