Message Boards Message Boards

0
|
3912 Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Do symbolic calculations with expressions full of QuantityVariables?

Posted 7 years ago

I have to get the derivate of a long expression full with QuantityVaribles. Here is a small part from it:

In[20]:= rbmmsp[{x, y, z}, {a, b, c}][[3, 1, 1, 1]]

Out[20]= -(QuantityVariable["b","Length"]/2) - QuantityVariable[
 "y","Length"] + Sqrt[(-(QuantityVariable["a","Length"]/2) - 
    QuantityVariable[
    "x","Length"])^2 + (-(QuantityVariable["b","Length"]/2) - 
    QuantityVariable[
    "y","Length"])^2 + (-(QuantityVariable["c","Length"]/2) - 
    QuantityVariable["z","Length"])^2]

When I take the derivate of it, it gives me 0.

In[21]:= D[rbmmsp[{x, y, z}, {a, b, c}][[3, 1, 1, 1]], x]

Out[21]= 0

Well, if I do it with normal variables:

In[3]:= D[-b/2 - y + 
  Sqrt[(-a/2 - x)^2 + (-b/2 - y)^2 + (-c/2 - z)^2] , x]

Out[3]= -((-(a/2) - 
  x)/Sqrt[(-(a/2) - x)^2 + (-(b/2) - y)^2 + (-(c/2) - z)^2])

I have a different result. How can I get the same with QuantityVariables ? Thanks ahead, János

POSTED BY: Janos Lobb
5 Replies

The way that they suggest to use units in formula expressions is this:

FormulaData["KineticEnergy", {QuantityVariable["K", "Energy"] -> 
   Quantity[10, "Joules"], 
  QuantityVariable["m", "Mass"] -> Quantity[10, "Grams"]}]

to define a function you would do something like this:

testfun[energy_, mass_] := 
 FormulaData[
  "KineticEnergy", {QuantityVariable["K", "Energy"] -> energy, 
   QuantityVariable["m", "Mass"] -> mass}]

To use it, you can either use numbers (but you lose your unit checking) or Quantity expressions:

testfun[Quantity[10., "Joules"], Quantity[10., "Grams"]]

to get the output form of this (copied as InputForm):

QuantityVariable["v","Speed"] == 
  Quantity[-44.7214, ("Meters")/("Seconds")] || 
 QuantityVariable["v","Speed"] == 
  Quantity[44.7214, ("Meters")/("Seconds")]

of course you can use any units:

testfun[Quantity[100./7457, "Hp Seconds"], 
 Quantity[10., "Grams"]]

gives the same result.

If you put numbers in but have the wrong units you will get the wrong answer.

For example:

testfun[10., 10.]

gives +-1.4 as the answer (which is wrong)

Therefore I'd usually recommend that you specify that the inputs must have units so Mathematica will enforce the proper units on your answer and you get an error if you make a mistake

testfun2[energy_Quantity, mass_Quantity] := 
 FormulaData[
  "KineticEnergy", {QuantityVariable["K", "Energy"] -> energy, 
   QuantityVariable["m", "Mass"] -> mass}]

HOWEVER,

with your nasty expression, I notice that when you use units it takes a long time to evaluate and it is instantaneous when you use numbers. This is maybe due to the fact that it has Log[length units] (which I really do not understand why??]. If you are careful to use consistent units it will evaluate much faster if you use the testfun version and not the testfun2. You will need to experiment with this to try to get the units version to evaluate faster.

POSTED BY: Neil Singer
Posted 7 years ago

Neil,

When I want to use the function I get after the partial derivations, I run into a substitution problem wether I use plain or Quantity wrapped constants.

Evaluate[Norm[rbmmf[{0, y, z}, {1, 1, 2}]]]

or

Evaluate[Norm[
  rbmmf[{Quantity["0", "Meters"], y, z}, {Quantity["1", "Meters"], 
    Quantity["1", "Meters"], Quantity["2", "Meters"]}]]]

The result still shows a,b,c as "quantity variable: length" or something similar. Any good hint how to supply concrete values to a function that has in its expression QuantityVariables ?

Thanks ahead, János

Attachments:
POSTED BY: Janos Lobb

Maybe you can define a variable like xvar = QuantityVariable["x", "Length"] and use xvar, yvar, and zvar to simplify your equations visually.???

POSTED BY: Neil Singer
Posted 7 years ago

Neil,

Yes. I guess from here on, if I work with curated data from physics, I have to watch out for these wrappers. It would be nice if Mathematica could put up a message for variable type mismatch, instead of the lame 0. Thanks a lot, János

POSTED BY: Janos Lobb

Janos,

your variable is the quantity variable (not "x") so you need to differentiate wrt that variable.

D[rbmmsp[{x, y, z}, {a, b, c}][[3, 1, 1, 1]], QuantityVariable["x", "Length"]]

will work.

Regards

POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract