Announcement

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

  • Using -solvenl- to solve a system of non-leanear equation with 2 equations

    I am trying to solve a simultaneous equation problem. Here is my code:


    Code:
    mata
    mata clear
    void function myfun(real colvector y, real colvector x) 
    {
        x[1] = (1/(4*(y[2])))
        y[2] = x[1]
    }    
    S = solvenl_init()
    solvenl_init_evaluator(S, &myfun())
    solvenl_init_type(S, "fixedpoint")
    solvenl_init_technique(S, "gaussseidel")
    solvenl_init_numeq(S, 2)
    solvenl_init_startingvals(S, J(2, 1, 1))
    solvenl_init_iter_log(S, "on")
    solvenl_solve(S)
    solvenl_result_values(S)
    end
    Stata returns error:
    HTML Code:
    missing values encountered when evaluating function
    r(416);
    And proceeds to display wrong results:

    HTML Code:
    1 and 0.25
    What I am doing wrong?

    Help would be much appreciated.

  • #2
    You need to specify both x[1], x[2], y[1] and y[2] in the function and use the damped Gauss Seidel technique. You will need to specify both positive and negative starting values to get both solutions.

    Code:
    . mata:
    ------------------------------------------------- mata (type end to exit) ---
    : mata clear
    
    : void function myfun(real colvector y, real colvector x) 
    > {
    >     x[1] = 1/(4*y[2])
    >     x[2] = y[1]
    > }    
    
    : S = solvenl_init()
    
    : solvenl_init_evaluator(S, &myfun())
    
    : solvenl_init_type(S, "fixedpoint")
    
    : solvenl_init_technique(S, "dampedgaussseidel")
    
    : solvenl_init_numeq(S, 2)
    
    : solvenl_init_startingvals(S, J(2, 1, 1))
    
    : solvenl_init_iter_log(S, "off")
    
    : solvenl_solve(S)
                     1
        +---------------+
      1 |  .4999999991  |
      2 |  .4999999996  |
        +---------------+
    
    : end
    --------

    Comment


    • #3
      Thanks Scott!

      Comment


      • #4
        Hi Scott,

        I am also interested in using solvenl to solve a system of non-linear equations with multiple solutions. What do you mean by “need to specify both positive and negative starting values to get both solutions”, how can we write that in the code because in your code, only the positive solution was displayed? Thanks!

        Comment


        • #5
          After doing some more research, I found out how to include the second guess for initial values in the code. I put it here to save a bit of time for those who may be interested:

          Code:
          : mata
          
          : 
          : ------------------------------------------------- mata (type end to exit) ------: 
          
          
          : 
          :  void function myfun2(real colvector x, real colvector values)
          > 
          >          {
          > 
          >                  values[1] = 9 - x[1]^2
          > 
          >                  values[2] = 4 - x[2]^2
          > 
          >          }
          
          : 
          :  
          : 
          :          S = solvenl_init()
          
          : 
          :          solvenl_init_evaluator(S, &myfun2())
          
          : 
          :          solvenl_init_type(S, "zero")
          
          : 
          :          solvenl_init_technique(S, "newton")
          
          : 
          :          solvenl_init_numeq(S, 2)
          
          : 
          :          solvenl_init_startingvals(S, J(2,1,1))
          
          : 
          :          solvenl_init_iter_log(S, "on")
          
          : 
          :          x = solvenl_solve(S)
          Iteration 0:  function =        73
          Iteration 1:  function = .83891649  delta X = 1.0161491
          Iteration 2:  function = .00016883  delta X = .08634441
          Iteration 3:  function = 2.301e-11  delta X = .00053125
          
          : x
                           1
              +---------------+
            1 |  3.000000796  |
            2 |  2.000000104  |
              +---------------+
          
          : solvenl_init_startingvals(S, J(2,1,-1))
          
          : 
          : solvenl_init_iter_log(S, "on")
          
          : 
          : x = solvenl_solve(S)
          Iteration 0:  function =        73
          Iteration 1:  function = .83891649  delta X = 1.0161491
          Iteration 2:  function = .00016883  delta X = .08634441
          Iteration 3:  function = 2.301e-11  delta X = .00053125
          
          : x
                            1
              +----------------+
            1 |  -3.000000796  |
            2 |  -2.000000104  |
              +----------------+

          Comment


          • #6
            Hello,

            Still about using solvenl to solve a system of nonlinear equations, I encounter the same error as Yonatan in post #1, but Mata stops and doesn't give anything:

            Code:
            missing values encountered when evaluating function
            r(416);
            Here is my code:

            Code:
            :
            
            : mata clear
            
            : 
            : 
            : 
            : void function euler6(real colvector k, real colvector euler)
            > 
            >    {
            > 
            > 
            > 
            > 
            > 
            > euler[1] = 1.93031370099921 - k[1] - (k[7] - k[2])*0.291*k[7]/k[1]
            > 
            > euler[2] = k[7] - k[2] - (k[8] - k[3])*0.291*k[8]/k[2]
            > 
            > euler[3] = k[8] - k[3] - (k[9] - k[4])*0.291*k[9]/k[3]
            > 
            > euler[4] = k[9] - k[4] - (k[10] - k[5])*0.291*k[10]/k[4]
            > 
            > euler[5] = k[10] - k[5] - (k[11] - k[6])*0.291*k[11]/k[5]
            > 
            > euler[6] = k[11] - k[6] - (k[12] - 0.5578397079)*0.291*k[12]/k[6]
            > 
            > euler[7] = k[7] - (1+2/(1+k[1]))*k[1]^0.3 - k[2]
            > 
            > euler[8] = k[8] - (1+2/(1+k[2]))*k[2]^0.3 - k[3]
            > 
            > euler[9] = k[9] - (1+2/(1+k[3]))*k[3]^0.3 - k[4]
            > 
            > euler[10] = k[10] - (1+2/(1+k[4]))*k[4]^0.3 - k[5]
            > 
            > euler[11] = k[11] - (1+2/(1+k[5]))*k[5]^0.3 - k[6]
            > 
            > euler[12] = k[12] - (1+2/(1+k[6]))*k[6]^0.3 - 0.5578397079
            > 
            > 
            > 
            >     }
            
            : 
            :  
            : 
            :          S = solvenl_init()
            
            : 
            :          solvenl_init_evaluator(S, &euler6())
            
            : 
            :          solvenl_init_type(S, "zero")
            
            : 
            :          solvenl_init_technique(S, "broydenpowell")
            
            : 
            :          solvenl_init_numeq(S, 12)
            
            : 
            :  solvenl_init_startingvals(S, (0.6\0.6\0.6\0.6\0.6\0.6\2\2\2\2\2\2))
            
            : 
            :  solvenl_init_conv_nearzero(S, 1e-16)
            
            : 
            :          solvenl_init_iter_log(S, "on")
            
            : 
            :          k = solvenl_solve(S) 
            Iteration 0:  function = 1.6522806
            Iteration 1:  function = .45259355  delta X = .47082007
            Iteration 2:  function = .45457711  delta X = .00687278
            Iteration 3:  function = 4.5859738  delta X = .74113661
            Iteration 4:  function = 8.9994886  delta X = .18547384
            Iteration 5:  function = 11.486567  delta X = .07186471
            Iteration 6:  function =  13.12673  delta X = .04244526
            Iteration 7:  function = 100.32277  delta X = 1.0785913
            Iteration 8:  function = 8.3213952  delta X = .58068121
            Iteration 9:  function = 2.1166884  delta X =  .3083803
            Iteration 10: function = 14.129653  delta X = .70395442
            Iteration 11: function = 44.427498  delta X = .48589505
            Iteration 12: function = 5.7743389  delta X = .48536736
            Iteration 13: function = .81969911  delta X =  .4080127
            Iteration 14: function = 9.2861825  delta X = .94467443
            Iteration 15: function = 19.573921  delta X = .27038551
            Iteration 16: function = 2.8390299  delta X = .43690324
            missing values encountered when evaluating function
            r(416);
            I would be much grateful if someone can help me fix this error.

            Comment

            Working...
            X