Jump to content

I suck at math need help.


Recommended Posts

So im trying to recreate this tutorial from the wiki. Specifically im trying to calculate dv for a transfer, but I cant get the right values. I have gone over and over the provided equations and I dont get the delta v that the author does.

 

I am building my calculations into a web page that you can find here

The calculations are all done in javascript so you can view the source but here are the two equations.
 

dv1 = Math.sqrt(u/r1)*(Math.sqrt(2*r2/r1+r2)-1);
dv2 = Math.abs(Math.sqrt(u/r2)*(1-Math.sqrt(2*r1/r1+r2)));

prior to these two lines of code im adding Kerbins R to r1 and r2 but that doesn't change the outcome I get. Other then that I think my equations are the same as in the tutorial. Can anybody figure out what im doing wrong?

Link to comment
Share on other sites

I doubt this is THE problem, but I see Math.abs in your second formula but not in the original tutorial.

 

I don't see any error in your formula, but I tried your web page and can clearly see the different answers you are getting.  I tested the original formulas myself and got the same answers as in the tutorial (after multiplying by 1k, which the original author omitted).  If I were doing this project and seeing these clearly incorrect answers coming out, I would add all of the terms in the calculation to the web page's output to make sure they are the values expected.  I suspect that somewhere your values for u, r1, or r2 are getting changed without your knowledge or approval.  Here's how I set it up in Excel:

 

mu 3530.461
r1 100
r2 200
R 600
   
R1=(r1+R) 700
R2=(r2+R) 800
   
T1=sqrt(mu/R1) 2.245777
T2=sqrt(mu/R2) 2.100732
T3=sqrt(2*R2/R1+R2)-1 0.032796
T4=1-sqrt(2*R1/R1+R2) 0.033908
   
dv1=T1*T3 73.65152
dv2=T2*T4 71.23209

 

Hope this helps!

 

Danny

Link to comment
Share on other sites

OK you all had good points.

I removed the math.abs first off. I only put that in there to test. Its not necessary once the equation is fixed.

Secondly r1+r2 did need to be changed to (r1+r2) and thanks to Danny I figured out that parenthesis were needed elsewhere.

This is how the correct eq's look.

dv1 = (Math.sqrt(u/r1))*(Math.sqrt(2*r2/(r1+r2))-1)
dv2 = (Math.sqrt(u/r2))*(1-Math.sqrt(2*r1/(r1+r2)))

Apparently javascript was messing the old high school order of operations up, and just doing things from left to right or something messed up like that. So I didn't change the equation much I just got all the parenthesis put in the right place and it works.

Thanks everyone. Now its time to work on the page. Im going to add some of the other calculators and then make it look more kerbalish'

 

Thanks again, Hal

Edited by halbert5150
Link to comment
Share on other sites

I did the parenthesis around r1+r2 first and that didn't solve it completely; although it did have to be used. I then noticed on your spreadsheet example for the final step to calculate dv1 and dv2 was multiplication. So I really did need all of the above suggestions to get it right.

So this,
Math.sqrt(u/r1) * (Math.sqrt(2*r2/(r1+r2))-1)

also had to have this,

(Math.sqrt(u/r1)) * (Math.sqrt(2*r2/(r1+r2))-1)

added to work right.

 

I think JS was actually doing this,

Math.sqrt(u)/Math.sqrt(r1) * ... which yielded .06889

or possibly this,

Math.sqrt((u/r1)) * ...  which also yields .06889

So Im not sure why what I did worked or what difference it made in the order of operations, but it does work.

I also wrote this same program in python and php just out of curiosity with the same .06889 calculated. Then added the same fix and both language versions started working once that was done.

Edited by halbert5150
Link to comment
Share on other sites

So im on my lunch break and it suddenly donned on me what my error was. I was looking at Math.sqrt() as a mathematical process and so part of the natural order of operations. It's not its a function call. Any programming language is going to do all math from left to right and then function calls from left to right. Which removes the square root calls from the natural order of op. The parenthesis force the compiler to make all the function calls at the right time and then finish up with the math. I didn't do a trace on the program but I know thats it.

 

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...