views:

336

answers:

2

I would like to pass the parameter values in meters or kilometers (both possible) and get the result in meters/second.

I've tried to do this in the following example:

u = 3.986*10^14 Meter^3/Second^2;
v[r_, a_] := Sqrt[u (2/r - 1/a)];

Convert[r, Meter];

Convert[a, Meter];

If I try to use the defined function and conversion:

a = 24503 Kilo Meter;
s = 10198.5 Meter/Second;
r = 6620 Kilo  Meter;
Solve[v[r, x] == s, x]

The function returns the following:

{x -> (3310. Kilo Meter^3)/(Meter^2 - 0.000863701 Kilo Meter^2)}

which is not the user-friendly format.

Anyway I would like to define a and r in meters or kilometers and get the result s in meters/second (Meter/Second).

I would be very thankful if anyone of you could correct the given function definition and other statements in order to get the wanted result.

+2  A: 

Here's one way of doing it, where you use the fact that Solve returns a list of rules to substitute a value for x into v[r, x], and then use Convert, which will do the necessary simplification of the resulting algebraic expression as well:

With[{rule = First@Solve[v[r,x]==s,x] 
      (* Solve always returns a list of rules, because algebraic 
         equations may have multiple solutions. *)},
  Convert[v[r,x] /. rule, Meter/Second]]

This will return (10198.5 Meter)/Second as your answer.

Pillsy
+1  A: 

You just need to tell Mathematica to simplify the expression assuming that the units are "possitive", which is the reason why it doesn't do the simplifications itself. So, something like

SimplifyWithUnits[blabla_, unit_List]:= Simplify[blalba, (#>0)&/@unit];

So if you get that ugly thing, you then just type %~SimplifyWithUnits~{Meter} or whatever.