Message Boards Message Boards

0
|
7193 Views
|
6 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Add units to variables inside a Manipulate expression?

Posted 5 years ago

I''ve created a Manipulate expression with many variables. I would like each of these variables to have associated units as in the standard Quantity[Value,"Unit"] so that I can easily enter the values I want and get the answer in correct units. Unfortunately, Manipulate doesn't seem to tolerate this regardless of where I try to insert this feature. I'd also like to add UnitConvert to the answer so that I can see it in "mL/min".

Manipulate[
 (\[Pi] (d/2)^2 c \[Epsilon]^3 Rb^2) ((3 (1 - \[Epsilon]) 0.0728 Cos[\[Theta]])/(\[Epsilon] L Rb) + 
     9800 Sin[\[Phi]]))/((1 - \[Epsilon])^2 0.00089),
    {{L, 0.07, "wick length"}, 0.01,0.1},
    {{d, 0.0068, "wick diameter"}, 0.001, 0.008},
    {c, 1/60, 1/30},
    {{\[Epsilon], 0.5, "porosity"}, 0.3,0.9},
    {{Rb, 0.0001, "fiber radius"}, 0.00001,0.0002},
    {{\[Theta], (70 \[Pi])/180, "contact angle"}, 0, \[Pi]/2},
    {{\[Phi], \[Pi]/2, "wick orientation"}, 0, \[Pi]/2}]

I've already tried replacing the bounds on each variable with quantities. And I've tried defining the symbols as quantities before entering the manipulate expression. I've also tried adding Quantity[] to each variable within the math expression. None of these works. Please help, there must be a way to do this, right?!

6 Replies

This was very helpful! I think my issues were incorrect Quantity syntax and the added complication of not defining my function before using Manipulate (I've attached a notebook file where I did this wrong). I've also attached the notebook file that gives me what I wanted based on your example. I defined some of the parameters outside of the Manipulate expression instead of putting them in as unvarying quantities as you did, but this seems to also work. Do you see any potential problem with this method or are they equivalent?

I was having trouble with your definition of gravity, so I just replaced it with the internal definition of the physical constant. The manipulate appears to work as intended.

gamma = ChemicalData["Water", "SurfaceTension"];
mu = ChemicalData["Water", "Viscosity"];
rho = ChemicalData["Water", "Density"];
g = Quantity["StandardAccelerationOfGravity"];
darcyEqn[d_, c_, eps_, Rb_, L_, theta_, 
  phi_] := ((Pi (d/2)^2 c eps^3 Rb^2)/((1 - 
         eps)^2 mu)) (((3 (1 - eps) gamma Cos[theta])/(eps L Rb)) + 
    g rho Sin[phi])
Manipulate[
 UnitConvert[
  darcyEqn[d Quantity[1, "Millimeters"], c, eps, 
   Rb Quantity[1, "Microns"], L Quantity[70, "Millimeters"], 
   theta \[Degree], phi \[Degree]], 
  "mL/min"], {{L, 70, "wick length"}, 1, 
  100}, {{d, 6.8, "wick diameter"}, 1, 8}, {c, 1/60, 1/
  30}, {{eps, 0.5, "porosity"}, 0.3, 0.9}, {{Rb, 100, "fiber radius"},
   10, 200}, {{theta, 70, "contact angle"}, 0, 
  90}, {{phi, 90, "wick orientation"}, 0, 90}]

Units with corrected manipulate

POSTED BY: Tim Laska

Thanks Tim. That is a much less problematic way of defining 'g'

Hey Tim Thanks for your reply. I altered my code slightly to make it more readable and may have accidentally removed a bracket. It evaluates correctly in my notebook document. I'm confident that's not the issue here. I have tried the ctrl= method of adding units with the same level of success as using the Quantity[] syntax. Sorry, I didn't mention that in my original post, I'd forgotten about that. Bill

Hi William,

Since I am not quite sure about your equation and where you want to put quantities, I threw together this example of calculating capillary rise in inches with a variety of input units using Units Discovery and Manipulate:

capillaryRise[gamma_, theta_, rho_, g_, r_] := 
 2 gamma Cos[theta] / (rho g r)
Manipulate[
 UnitConvert[
  capillaryRise[gamma Quantity[1, (("Dynes")/("Centimeters"))], 
   theta \[Degree], Quantity[1, ("Grams")/("Centimeters")^3], 
   Quantity[9.8, ("Meters")/("Seconds")^2], r Quantity[1, "Microns"]],
   "inch"], {gamma, 1, 80}, {theta, 0, 180}, {r, 1, 1000}]

Units Discovery Example

Does this example help?

POSTED BY: Tim Laska

When I copied your Manipulate expression, Mathematica indicates that the your parentheses are unbalanced (one more right paren than left). You will need to check that first.

Second, you may want to use the Unit Discovery feature by typing ctrl+=. You may want to check out my post https://community.wolfram.com/groups/-/m/t/1560271?ppauth=e8bMDzPu for an example of using Units Discovery and UnitConvert.

POSTED BY: Tim Laska
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