I'm having some problems making plots with quantities in Mathematica Online 11.3.0.
If I make a plot that is defined with the $x$-axis as an independent quantity (e.g., 0 to 3 seconds) and the $y$-axis as a dependent quantity (e.g., 0 to 100 meters, dependent on $x$), I get reasonable results. However, if I try to do unit conversions in my Plot
call, I get an empty plot.
I suspect that I'm missing something about the evaluation order, but I'm not sure what. The question Can't force UnitConvert to be evaluated in Plot is related, but it's in the context of Mathematica 9. I'm using 11, and it seems that the way Plot handles units was changed significantly in 10; see https://www.wolfram.com/mathematica/new-in-10/enhanced-visualization/plot-functions-using-units.html
(By the way: I don't get the units next to my axes like that Wolfram post shows, and that's another matter.)
The following sample session demonstrates the problem I'm having. Here, I plot the descent of a ball dropped from a 100 m building. I demonstrate that it works fine when the $x$ and $y$ axes are Quantity
objects, but if I make the Plot
argument a UnitConvert
expression, it's empty. Other things I tried are listed below.
In this example, s
is the standard constant-acceleration equation, $s=a t^2 + v_0 t + s_0$, for freefall from a 100m tower, with $a = -9.8\ \frac{\text{m}}{\text{s}^2}$, $v_0=0\ \frac{\text{m}}{\text{s}}$, and $s_0 = 0\ \text{m}$.
(* Let s be the height of a ball dropped from a 100m tower, after t seconds. *)
In[1]:= s=Quantity[100,"Meters"]-Quantity[9.8,"Meters/Seconds^2"]*t^2
2
Out[1]= t -9.8 meters per second squared + 100 meters
(* Test: Where is it after three seconds? *)
In[2]:= s/.t->Quantity[3,"Seconds"]
Out[2]= 11.8 meters
(* Test that UnitConvert can convert this to feet. *)
In[3]:= UnitConvert[s/.t->Quantity[3,"Seconds"],"Feet"]
Out[3]= 38.7139 feet
(* Plot the ball's path. This lets us confirm that Plot is able to use unit-based data on both axes. *)
In[4]:= Plot[s,{t,Quantity[0,"Seconds"],Quantity[3,"Seconds"]}]
Out[4]=
(* Plot the ball's path, but in feet. *)
In[5]:= Plot[UnitConvert[s,"Feet"],{t,Quantity[0,"Seconds"],Quantity[3,"Seconds"]}]
Out[5]=
I don't show it here, but the same thing happens if I try these:
- Use
QuantityMagnitude[s, "Feet"]
instead ofUnitConvert[s, "Feet"]
: i.e., try to do the conversion but into a scalar instead of aQuantity
. - Define
s
as a function instead; and plotUnitConvert[s[t], "Feet"]
. - Define
sf=UnitConvert[s,"Feet"]
and plotsf
. - Plot
Evaluate[UnitConvert[s,"Feet"]
. - Set
Evaluated->True
in thePlot
options.
Aside:
The original motivation for this, of course, wasn't this case. I was trying to plot (after substitutions) Plot[Sqrt[Quantity[5771, ("Milliamperes"*"Volts")/"Megahertz"] / l], {l, Quantity[1,"uH"], Quantity[22,"uH"]}]
. However, the native units for this computation's result ($\frac{\sqrt{\text{mA}} \sqrt{\text{V}}} {\sqrt{\text{MHz}}\sqrt{\text{μH}}}$) are different from amps by a factor of $\frac{1}{10\sqrt{10}}$. So, I wanted to convert everything to milliamps to make it easier for me to deal with, but found out that doing the conversion gave me a blank plot.