3
$\begingroup$

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}]

Plots

Can anyone explain why it does that?

$\endgroup$
4
  • $\begingroup$ I don't know if this is the cause, but what exactly do you expect the square root of a centimeter to mean? $\endgroup$
    – lericr
    Commented Oct 12, 2022 at 20:42
  • $\begingroup$ 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]) $\endgroup$ Commented Oct 12, 2022 at 20:54
  • $\begingroup$ 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. $\endgroup$
    – lericr
    Commented Oct 12, 2022 at 21:02
  • $\begingroup$ 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. $\endgroup$
    – lericr
    Commented Oct 12, 2022 at 21:09

1 Answer 1

4
$\begingroup$
$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

enter image description here

Plot[Evaluate@f[zz cm], {zz, -5, 5}] // AbsoluteTiming

enter image description here

The second plot should not be evaluated

Plot[f[zz], {zz, -5 cm, 5 cm}, Evaluated -> False]

enter image description here

The third plot is not expected to be the same.

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$
    – lericr
    Commented Oct 12, 2022 at 22:11
  • $\begingroup$ 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. $\endgroup$
    – lericr
    Commented Oct 12, 2022 at 23:34
  • $\begingroup$ @lericr - Evaluated is just something that I remembered. Options[Plot] indicates that the default is Evaluated -> Automatic $\endgroup$
    – Bob Hanlon
    Commented Oct 13, 2022 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.