# Define functions for variables with units?

Posted 2 years ago
4560 Views
|
15 Replies
|
2 Total Likes
|
 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: Answer
15 Replies
Sort By:
Posted 2 years ago
 You mean something like this? d[t_]:= Quantity[20,"Meters"/"Seconds"^2]t^2/2+ Quantity[5,"Meters"/"Seconds"]t+ Quantity[25,"Meters"] In:= d[Quantity[13, "Seconds"]] Out= Quantity[1780, "Meters"] In notebook it will look like:  Answer
Posted 2 years ago
 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... Answer
Posted 2 years ago
 Thank you!GOGO Answer
Posted 8 months ago
 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 Answer
Posted 8 months ago
 Try this... 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: Answer
Posted 8 months ago
 Try this... 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: Answer
Posted 8 months ago
 Thank you very much! Why does this not work?In:= 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: Answer
Posted 8 months ago
 Try adjusting the arguments passed to your Plot function to this... Plot[z[Quantity[t,”Seconds”]],{t,0,10}] All the best,Ian Answer
Posted 8 months ago
 That works! Thank you! Is there anywhere in the documentation I can find all this?GOGO Answer
Posted 8 months ago
 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 Answer
Posted 8 months ago
 Thanks. It can get pretty confusing.GOGO Answer
Posted 8 months ago
 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: Answer
Posted 8 months ago
 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 Answer
Posted 8 months ago
 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: Answer
Posted 8 months ago
 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. Answer