4
$\begingroup$

I've been trying to find some information on how to use Plot with a quantity that has units. I have a function, h[x_], that takes as input a value with units of centimeters and produces as output a value with units of Amperes per centimeter (I'd like it to output in Oersteds but that's a different question for a different time). I want to plot the value of h[x] as a fraction of the value of Hinf (a quantity with dimensions of Amperes per centimeter). What I am trying is

h[x_] := 4 Pi/10*Quantity[2, "Amperes"]*960/(Quantity[60.96, "Centimeters"])*(((Quantity[60.96, "Centimeters"]) + 2 x)/(2 Sqrt[(Quantity[2.54, "Centimeters"])^2 + (Quantity[60.96, "Centimeters"] + 2 x)^2]) + ((Quantity[60.96, "Centimeters"]) - 2 x)/(2 Sqrt[(Quantity[2.54, "Centimeters"])^2 + (Quantity[60.96, "Centimeters"] - 2 x)^2]))
Hinf := 4 Pi/10*960/Quantity[60.96, "Centimeters"]*Quantity[2, "Amperes"]
Plot[Evaluate[h[x]/Hinf], {x, Quantity[0, "Centimeters"], Quantity[30.48, "Centimeters"]}]

which returns an error message, along with a vastly incorrect plot (this should be the magnetic field intensity inside of a solenoid, but instead seems to be something completely different?).

I actually input the various quantities using the ctrl-= Wolfram Alpha interface, just in case that would help to know.

error message and plot

I have no idea what I did wrong here that's making Mathematica give me this mess instead of what I asked for, and I can't figure out how to interpret the error message. How can I get it to plot properly?

A bit of further information:

Evaluate[h[Quantity[0, "Centimeters"]]/Hinf]

does indeed output the correct value, and this does work for any value as the argument of h.

$\endgroup$

1 Answer 1

9
$\begingroup$

Look at what this gives:

Evaluate[h[x]/Hinf]

enter image description here

This is clearly wrong, you're adding x of no quantity to a quantity of Centimeters, rather than parsing the units in the plotdomain, try it directly.

Plot[Evaluate[h[Quantity[x, "Centimeters"]]/Hinf], {x, 0, 30.48}, 
 AxesLabel -> {"cm", "Amperes"}]

enter image description here

$\endgroup$
1
  • $\begingroup$ I figured it would be able to parse dimensions in the domain. Guess I was wrong, then! Thanks for the help. $\endgroup$
    – Hearth
    Sep 4, 2016 at 20:10

Your Answer

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

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