Jump to content

Computing escape velocity and density in php. Code included.


MGCJerry

Recommended Posts

This isn't KSP related, but definitely space related. I have an old script ive been trying to modernize and I wanted to add some calculations to help fill in missing data. Escape Velocity and volume. The problem is with the solutions that php is giving me. Either I'm just being really stupid, or there's some bug in how php handles math.

Lets start with Earth. Its giving me all kinds of convoluted output. Its telling me Earth's escape velocity is 353420.83796328... I know its 11.200 km/s, rearranging the math and using brackets does not alter the output. I have been fighting this math on and off for the last 3 days. I can get it to get close, but when you try figuring it out for our sun, its over 10,000x over what the answer should be. I have even tried avoiding scientific notation from php, but even then the output remains the same.

Here's my variables...


$radius = 6378.1; //Earth radius
$mass = '5.972e+24'; //earth mass
$g = 6.67*pow(10, -11); // Gravity constant
$volume = sprintf("%01.5f", 4.19*$radius*$radius*$radius);
$density = $mass/$volume; //density..
$ev = sqrt(2*$g*$mass/$radius); // Escape velocity

$ev returns: 353420.83796328...

As far as density concerned, it give me and obnoxious number... 5493280854827.7

I have this bit of code that gives me correct information, but when it comes to the sun, it becomes extremely wrong. This is the code that is currently in the script that I managed to get to work after screwing with it for 2 days. Otherwise the numbers are very good out of it (even if the equations are wrong).


$g = 6.67*pow(10, -11); // Gravity constant
$volume = 4.19*($radius*$radius*$radius); // Volume
$d = $mass/$volume/1000000000000; // no idea why i have to divide like this... otherwise its stupid WRONG
$ev = sqrt(2*$g*$mass/($radius*1000)); // Escape velocity.. no idea why i need to divide by 1000.. its over 3 million, and stupid wrong if i dont.
$gv = ($g*$mass)/($radius*1000*$radius*1000); // Gravity
$buffer['escapeVelocityMS'] = number_format($ev, 2); // escape velocity in m/s
$buffer['escapeVelocityKMS'] = number_format($ev/1000, 3); // escape velocity from m/s to km/s
$buffer['gravityMS'] = number_format($gv, 2); // force of gravity
$buffer['relativeGravity'] = number_format($gv/9.80, 2); // gravity relative to earth
$buffer['volumeCM'] = number_format($volume, 0); //density in cubic meters

Using the code above, I get the following results... which I find acceptable.


Array
(
[escapeVelocityMS] => 11,179.98
[escapeVelocityKMS] => 11.180
[gravityMS] => 9.80
[relativeGravity] => 1.00
[volumeCM] => 1,087,095,173,817
)

When it comes to our sun... Here are the results it gives... ... Which are beyond wrong. Of course I'm using the sun's data to try to compute it.


Array
(
[escapeVelocityMS] => 61,765,610.61
[escapeVelocityKMS] => 61,765.611
[gravityMS] => 2,742,624.48
[relativeGravity] => 279,859.64
[volumeCM] => 1,409,630,947,936,250,112
)

Any ideas why i cant get the math to work the same for both items? I almost give up trying to hack at it myself. Thanks :)

Edited by MGCJerry
Link to comment
Share on other sites

The problem is you're not being careful with your units, you end up giving the body radius in kilometers, which is why you have "random" factors of 1000 being divided our multiplied out: because you want the radius to be specified in meters, because the value of the gravitational constant you're using has units of m³ / ( kg s² ), not km.

Also, don't mix in strings with numbers, that's really messy.

Here's a working version of the first script:

$radius = 6378100; //Earth radius 
$mass = 5.97219e+24; //earth mass
$G = 6.67408e-11; // Gravity constant
$volume = 4 * pi() * $radius * $radius * $radius / 3;
$density = $mass/$volume; //density..
$ev = sqrt(2* $G * $mass / $radius); // Escape velocity

I've also replaced the 4.19 magic number with what it actually should be: 4 À / 3

Link to comment
Share on other sites

Ah! that explains a lot. I've been feeding it km the whole time. I knew it was something stupid simple... I'm not a math guy so details like this slip by.

Typically I do not mix strings & numbers. I seen I did that after I posted it, it was my code where I was in the 'let me get the right numbers, then ill clean up' mode. Also because math isnt my strongest point, the 4.19 was one of the ways of trying to simplify it, but it did give me some good numbers... Until...

Thanks for the solution and pointing the idiot (me) to the right direction. :)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...