# Plotting a function with units gives two different results

With the following code block, I plot a function with units in it in two different ways. Both ways should do the same in my understanding of the documentation.

cm = Quantity["Centimeters"];
f[x_] := Sqrt[x + 5 cm]
Plot[Evaluate@{f[zz cm]}, {zz, -5, 5}]
Plot[Evaluate@{f[zz]}, {zz, -5 cm, 5 cm}]
Plot[Evaluate@{Sqrt[zz]}, {zz, -5 cm, 5 cm}] Can anyone explain why it does that?

• I don't know if this is the cause, but what exactly do you expect the square root of a centimeter to mean? Oct 12, 2022 at 20:42
• This is just to demonstrate the problem. Based on the documentation this should give me the plot of Sqrt[x+5] and the units shouldn't play a role for plotting. (I'm working on a physics problem with a more complicated formula, but there I basically have the pythagorean theorem, so it would be Sqrt[x^2+(5cm)^2]) Oct 12, 2022 at 20:54
• Okay, here's a guess. f[zz cm] gives Sqrt[zz*Quantity[1, "Centimeters"] + Quantity[5, "Centimeters"]]. This gives us the "good" plot. Notice that the units are explicit in both terms. On the other hand, f[zz] gives Sqrt[zz + Quantity[5, "Centimeters"]]. This gives us the "bad" plot. I wonder if not knowing what units zz will be in is confusing the Plot function. I don't know if this should be expected or not. Oct 12, 2022 at 21:02
• Testing the Sqrt issue, I replaced redefined f: f[x_] := 3 (x + 5 cm). Similar issue occurs, but it shows (I think) that the Quantity[5, "Centimeters"] is being ignored/dropped in the second plot. I think Plot must be trying to pre-compute its plotting function, and that 5cm is somehow not being interpreted correctly within that expression. I'm just making noise--we need someone with more expertise/knowledge. Oct 12, 2022 at 21:09

\$Version

(* "13.1.0 for Mac OS X x86 (64-bit) (June 16, 2022)" *)

Clear["Global*"]

cm = Quantity["Centimeters"];
f[x_] := Sqrt[x + 5 cm]


The first plot can be evaluated or not.

Plot[f[zz cm], {zz, -5, 5}] // AbsoluteTiming Plot[Evaluate@f[zz cm], {zz, -5, 5}] // AbsoluteTiming The second plot should not be evaluated

Plot[f[zz], {zz, -5 cm, 5 cm}, Evaluated -> False] The third plot is not expected to be the same.

• I don't see any documentation about Evaluated. Can you maybe expound on that a bit? In particular, Plot[f[zz], {zz, -5 cm, 5 cm}] fails to show anything, and I would have thought that was the "unevaluated" version. So, how does the Evaluated option differ from wrapping the plot function with Evaluate (or not), and can you point us to any useful documentation? Oct 12, 2022 at 22:11
• I've been poking around the Mathematica SE, and there are a few references to Evaluated, but details are sketchy. It also seems there has been bugginess in the past. Without any further explanation, I think my tendency would be to avoid this option altogether. Oct 12, 2022 at 23:34
• @lericr - Evaluated is just something that I remembered. Options[Plot] indicates that the default is Evaluated -> Automatic` Oct 13, 2022 at 2:23