Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with function definition for mm_root

    I'm attempting to find the root of a non-linear equation using mata. Running Stata 15.1 on MacOS Mojave 10.14.6. Here is my code:

    mata
    maxu = 2.63506
    u3 = 836.8054
    u4 = 117.3982
    u2 = 4.283902
    w = 60.069
    function myfunc(x, maxu, u3, u4, u2, w) return(maxu - ln(u3+u4+u2*exp(.0175134(w-x))))

    rc = mm_root(x=., &myfunc(), 0, 1, 0 , 1000, maxu, u3, u4, u2, w)
    x
    end

    This is the error message I'm getting:

    : function myfunc(x, maxu, u3, u4, u2, w) return(maxu - ln(u3+u4+u2*exp(.0175134(w-x))))
    invalid expression
    r(3000);

    Any suggestions? Thanks.

  • #2
    You are missing an operator in your returned expression. Perhaps
    Code:
    function myfunc(x, maxu, u3, u4, u2, w) return(maxu - ln(u3+u4+u2*exp(.0175134*(w-x))))
    is what you intended.

    Comment


    • #3
      Whoops. So simple. Thank you very much.

      Comment


      • #4
        I have it working, but mm_root is returning a missing value for the root (despite the fact that rc==0):

        mata
        maxu = 2.63506
        u3 = 836.8054
        u4 = 117.3982
        u2 = 4.283902
        w = 60.069
        function wtp(x, maxu, u3, u4, u2, w) return(maxu - ln(u3+u4+u2*exp(.0175134*(w+x))))

        rc = mm_root(x=., &wtp(), 0, 1000000, 0 , 10000, maxu, u3, u4, u2, w)
        rc
        x
        end

        Comment


        • #5
          Found another error. Root-finding is running great now.

          Comment

          Working...
          X