Message Boards Message Boards

0
|
14434 Views
|
15 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Define functions for variables with units?

Units are important in engineering and science, so I need to get my students to be religious about using them. I provide answers to homework and exams using Mathematica. Incorporating units in simple computations is relatively easy, but I have difficulty in using units with functions. For example how do I define a function

d[t_]:=10*t^2 + 5*t+25

where the units of t are seconds? In subsequent calls of the function, do I need to use the Quantity notation for every specific numeric value of t?

Attachments:
POSTED BY: Orest Gogosha
15 Replies

You mean something like this?

d[t_]:=
    Quantity[20,"Meters"/"Seconds"^2]t^2/2+
    Quantity[5,"Meters"/"Seconds"]t+
    Quantity[25,"Meters"]

In[1]:= d[Quantity[13, "Seconds"]]
Out[1]= Quantity[1780, "Meters"]

In notebook it will look like:

enter image description here

POSTED BY: Sam Carrettie

You could limit your function to only accept units of time:

ClearAll[d]
d[t : Quantity[_, unit_]?(CompatibleUnitQ[#, "Seconds"] &)] := 10*t^2
d[Quantity[5, "Seconds"]]

or, similarly, one could use UnitDimensions to check if the input has time-dimensions... Of course the 'variables' 10, 5, and 25 in your equation should have commensurate dimensions...

POSTED BY: Sander Huisman

Thank you!

GOGO

POSTED BY: Orest Gogosha

Belated "Thank You." This has been very useful in evaluating functions but I don't have enough knowledge of Mathematica to extend it to other uses. For example when I try:

Plot[d[t], {t, Quantity[0, "Seconds"], Quantity[10, "Seconds"]}]

I get axes but no plot. Is there a part of the documentation I should read?

GOGO

POSTED BY: Orest Gogosha

Try this...

enter image description here

A notebook is also attached.

Here I've evaluated the Quantity[...] functions in place as I think it makes documents more readable. On a Mac, this is done by selecting the part of the expression you want to evaluate in place and pressing CMD-SHIFT-ENTER. Once evaluated, you can click into the Quantity panel and edit the numeric value. Alternatively, you can get a plain unit (without a value) by using the expression Quantity[None,"Unitname"]. Using this approach you can keep the quantity value outside the quantity panel. An example is given at the end of the attached notebook.

Hope this helps,

Ian

Attachments:
POSTED BY: Ian Williams

Try this...

enter image description here

A notebook is also attached.

Here I've evaluated the Quantity[...] functions in place as I think it makes documents more readable. On a Mac, this is done by selecting the part of the expression you want to evaluate in place and pressing CMD-SHIFT-ENTER. Once evaluated, you can click into the Quantity panel and edit the numeric value. Alternatively, you can get a plain unit (without a value) by using the expression Quantity[None,"Unitname"]. Using this approach you can keep the quantity value outside the quantity panel. An example is given at the end of the attached notebook.

Hope this helps,

Ian

Attachments:
POSTED BY: Ian Williams

Thank you very much! Why does this not work?

In[30]:= ClearAll[all] z[t_] := Quantity[20, "Meters"/"Seconds"^2] t^2/2 + Quantity[5, "Meters"/"Seconds"] t + Quantity[25, "Meters"] z[Quantity[10, "Seconds"]] Plot[z[t "Seconds"], {t, 0, 10}]

Quantity[1075, "Meters"]

The plot has the correct horizontal axis, the vertical axis goes from +1 to -1, and there is no curve.

Attachments:
POSTED BY: Orest Gogosha

Try adjusting the arguments passed to your Plot function to this...

Plot[z[Quantity[t,”Seconds”]],{t,0,10}]

All the best,

Ian

POSTED BY: Ian Williams

That works! Thank you! Is there anywhere in the documentation I can find all this?

GOGO

POSTED BY: Orest Gogosha

Not sure. Try reading through the help pages for Plot and Quantity. The reason your code didn’t produce the desired outcome was that you omitted the Quantity function. Hence, the argument passed your user defined function z[] was treated as an expression corresponding to t*”Seconds”. As this expression evaluates to a non-numeric value, it couldn’t be plotted.

All he best,

Ian

POSTED BY: Ian Williams

Thanks. It can get pretty confusing.

GOGO

POSTED BY: Orest Gogosha

Here we go again! For some reason in the definition of velocity time is getting the units of s^2.

I figured it out.

Attachments:
POSTED BY: Orest Gogosha

I don't have time at the moment to go through your notebook in detail. However, I find the best way to develop a notebook (especially when the coding is more complex) is to build it up line-by-line or cell-by-cell and then, once I'm happy things are working as I expect, combine the cells into a single or compound expression. Breaking the code down also helps find problems when things go wrong.

One thing I see from a very quick scan of your code is that the function v11 is called recursively (i.e. v11 is called from within the function v11). You also quantify t when you call v11 from within v11 - this could be a problem if t had already been quantified when passed to the function v11 (in which case you'd be quantifying it twice). I'd also be careful about using subscripts for variable names - they may not be giving you what you think. I'd suggest looking at the Notation package and using the Symbolize function if you want to use subscripted variables. I'm pretty sure there's stuff on this site and StackExchange relating to this topic.

Hope this helps,

Ian

POSTED BY: Ian Williams

The recursive call was a typo. Things continue to evolve. If you have time, take a look. I may have hit a limitation on what some of the Solve functions can do. This NSolve works fine without units.

Attachments:
POSTED BY: Orest Gogosha

Might the problem be that the head of the variable x in your expression sol:=.... is "Symbol"? Do you need to give it a numeric value? As suggested previously, you might get better insight as to what's going wrong if you develop and evaluate the code line-by-line with each line in a separate cell.

POSTED BY: Ian Williams
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