Message Boards Message Boards

0
|
2724 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How can I get FindRoot to work with units?

Posted 1 year ago

One of the things I find most frustrating about programming languages is the way they use "type" checking rather than dimensional commensurability as the foundation. I mean relation tables have columns that, properly treated, can be seen as dimensions of measurements that implicitly resolves the "object relational impedance mismatch". So I was really happy to see Mathematica provided facilities for dimensional commensurability with units as a kind of "I/O format" for quantification.

But when I tried to use these essential features in a rigorous manner, I ran into a brick wall with FindRoot and found I had to not only convert everything to SI units but I had to strip off just the units with QuantityMagnitude everywhere, thereby exposing my notebook to the same old errors that arise when there is no checking.

It seems I must be doing something wrong here since such an essentially attractive feature of Mathematica in any real world application can't be disabled by a function so basic as FindRoot. So what am I doing wrong?

POSTED BY: James Bowery
4 Replies
Posted 1 year ago

Hm, right. It really seems like it cannot deal with a multivariable problem ... Here is a simpler example that produces the same error message:

FindRoot[{x == Quantity[1, "Seconds"], 
  y == Quantity[2, "Meters"]}, {{x, Quantity[3, "Seconds"]}, {y, 
   Quantity[4, "Meters"]}}]
(* FindRoot::nlu: -- Message text not found -- 
  ({3.s,4.m}) ({x,y}) ({2.s,2.m}) ({2}) (Removed[$$Failure]) *)
POSTED BY: Domen V
Posted 1 year ago

Thanks for finding a minimal example.

POSTED BY: James Bowery
Posted 1 year ago

Then perhaps I've found a bug in Mathematica's handling of Units with FindRoot because here's the opaque error message ("Message text not found") that appears when I don't strip off the Units with QuantityMagnitude:

enter image description here

POSTED BY: James Bowery
Posted 1 year ago

FindRoot does work with Quantity, but you have to provide the appropriate units for the variables. You can read the tutorial on Numerical Calculations with Units, which also provides an example:

FindRoot[1/
  2 (t^2 (Quantity[-9.8, ("Meters")/("Seconds")^2]) + 
    Quantity[200, "Meters"]), {{t, Quantity[1, "Seconds"]}}]

(* {t -> Quantity[4.51754, "Seconds"]} *)
POSTED BY: Domen V
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