Message Boards Message Boards

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

What is the proper format to use NSolve to solve equations with units?

Posted 8 years ago

Hi,

I know that Christmas Eve (2015) is supposed to be a full moon. I wanted to compute the time from 12/23/15 (~ 3:15 pm EST) to the full moon. I thought I might have been able to use NSolve and Now to solve for when MoonPhase[...]==1.

In[16]:= NSolve[{MoonPhase[Now + Quantity[x, "Days"]] == 1}, x]

During evaluation of In[16]:= MoonPhase::dtspec: Wed 23 Dec 2015 15:03:23GMT-5.+xdays is not a valid date specification. >>

During evaluation of In[16]:= MoonPhase::dtspec: Wed 23 Dec 2015 15:03:23GMT-5.+xdays is not a valid date specification. >>

During evaluation of In[16]:= MoonPhase::dtspec: Wed 23 Dec 2015 15:03:23GMT-5.+xdays is not a valid date specification. >>

During evaluation of In[16]:= General::stop: Further output of MoonPhase::dtspec will be suppressed during this calculation. >>

During evaluation of In[16]:= NSolve::units: NSolve was unable to determine the units of quantities that appear in the input. >>

Out[16]= NSolve[{MoonPhase[
    DateObject[{2015, 12, 23}, 
      TimeObject[{15, 3, 23.0076}, TimeZone -> -5.], 
      TimeZone -> -5.] + Quantity[x, "Days"]] == 1}, x]

Additionally, the following code does not work:

NSolve[{MoonPhase[Now + x*Quantity[1, "Days"]] == 1}, x]

Anyone have suggestions or is this just not an acceptable way to solve the problem? I know I can always just create an interpolating function of MoonPhase over the course of a few days, then solving for the time until the full moon from that data. However, I wanted to solve this directly and to know that limitations of using equations with units and/or functions that provide data (e.g. distances between locations, MoonPhases, etc.).

Thanks, Joe

POSTED BY: joseph pantina
2 Replies
Posted 8 years ago

Hi Sean,

I appreciate your reply and the comments on the code you posted. I had a feeling that there might have been an error in my initial approach due to the fact that I was trying to solve for an exact value (which theoretically should exist) based upon what I assume is only a discrete sampling of the underlying continuous function (i.e. the MoonPhase). I had actually tried solving for the MoonPhase == 0.99 as well because I was thinking it might never exactly reach 1, but that didnt address the underlying discrete sampling problem which is remedied by your maximization suggestion.

Alas, without knowing the exact time, I was still able to see the full moon for Christmas.

  • Joe
POSTED BY: joseph pantina

It's an interesting piece of code.

Fundamentally, the problem is that you don't want to search for when the illumination == 1. You want to search for when the illumination is maximized.

NMaximize[MoonPhase[Now + Quantity[x, "Days"]], x]

{0.998137, {x -> 0.716619}}

You'll see that this code gives the same messages. That's okay. These functions are trying to do things with the input (like finding derivatives). If you want NMaximize to treat this purely numerically, then you might run:

objectiveFunction[x_?NumericQ] := MoonPhase[Now + Quantity[x, "Days"]]
NMaximize[objectiveFunction[x], x]
POSTED BY: Sean Clarke
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