[R] Solving systems of non-linear equations in R

Spencer Graves spencer.graves at pdf.com
Thu Feb 24 00:14:58 CET 2005


      Write a driver function to compute the sum of squares of 
deviations from target.  If the nonlinear equations are not pathological 
and a unique solution exists, "optim" will find it.  If no solution 
exists, "optim" will find something close -- in terms of the sum of 
squared deviations.  If the functions have discontinuities, multiple 
solutions, or possibly other pathologies, I'd try all 4 methods (after 
transforming parameters to eliminate constraints).  You could also try 
to generate random starting points over a plausible region, evaluate 
your objective function at all the random starting points and feed the 
best one (or the best 10) to "optim". 

      How big is "n"?  If it's bigger than, say, half a dozen, I suspect 
you have a variance component situation, in which case, you would be 
wise to use nlme;  see Pinheiro and Bates (2000) Mixed-Effects Models in 
S and S-Plus (Springer).  Or try Scilab (http://scilabsoft.inria.fr). 

      hope this helps. 
      spencer graves

T Petersen wrote:

> Thank you, that was very helpful. My functions are in general 
> monotonic, continous and differentiable(one exception sometimes 
> encountered being y = min(a*x1,b*x2)) and do have a unique solution, 
> if you specify the problem correctly.
> I have never worked with non-liner solving algoritms in my math 
> courses, so maybe starting to setup algoritms in R may take the focus 
> away from my thesis subject. On the other hand it could be fun :-D
> Maybe I should check out the optim()-function again. You can solve 
> systems with more tha one equations with optim()?
>
> regards
>
> Spencer Graves wrote:
>
>>      A system of n equations in n unknowns has a unique solution if 
>> the n equations are linear and linearly independent.  If the system 
>> is nonlinear, then one must characterize the nonlinearity before 
>> saying anything about whether a solution exists and if so how many 
>> solutions are there?
>>      Example 1:  Solve sin(x)=0 for x.  Answer:  x = 2*n*pi, for n = 
>> any integer.
>>      Example 2:  Solve sin(x) = 2 for x.  Answer:  If x must be a 
>> real number, then this equation has no solutions.
>>      Are your functions monotonic?  Continuous?  Differentiable?
>>      Without getting into pathologies like the Cantor function (e.g., 
>> http://www.cut-the-knot.org/do_you_know/cantor.shtml), my experience 
>> with a variety of practical problem like this suggests that it is 
>> best to recast the problem as one of minimizing, e.g., the sum of 
>> squared deviations from target.  Moreover, I've had good luck 
>> transforming the parameter space to eliminate constraints -- or 
>> incorporating the constraints into the objective function and then 
>> solving the superficially unconstrained problem.  If my functions 
>> have singularities where I might get 0/0 or Inf-Inf, for example, I 
>> use asymptotic expansions to "approximate" the function(s) near the 
>> singularities more accurately than can be achieved with any 
>> finite-precision arithmetic.
>>      With all of this, I'm confident that there are better algorithms 
>> than the different methods in "optim", but I don't have not had the 
>> need to hunt for them.  The methods in "optim" provide a reasonable 
>> range of options for the problems I've encountered.
>>      The R project has another advantage over a commercial software:  
>> You can see the source code.  You can trace it step by step and find 
>> out where it does not work well for the specific problems you 
>> consider.  If you're clever, you might be able to find a way to 
>> improve that algorithm and make it part of your thesis -- and get a 
>> publication on it in some statistical software journal.  Where else 
>> can you so easily climb up and stand on the shoulders of giants?  If 
>> you find a platform for innovation better than R, please let me know.
>>      hope this helps.      spencer graves   T Petersen wrote:
>>
>>> No, this doesn't seem right. What I look for is something that could 
>>> solve nonlinear systems with n unknowns and n equations. So there 
>>> will be zero degrees of freedom, and statistical methods can't be 
>>> the right way forward.
>>>
>>> Specifically  I can see that the litterature mentions's "Scarf's 
>>> algoritm" (Scarf 1967) and Merril's refinement of Scarf's algoritm 
>>> in 1972, but there might be other algoritms too...
>>>
>>> Regards...TP
>>>
>>> yutaka hamaoka wrote:
>>>
>>>>
>>>> I believe
>>>> library(systemfit)
>>>> has nlsytemfit function.
>>>>
>>>> Yh
>>>>
>>>>
>>>> T Petersen wrote:
>>>>
>>>>> I'm about to write my thesis in economics and will need to setup 
>>>>> and solve a system of non-linear equations. At our university we 
>>>>> usually use GAMS for this, and though GAMS is a fine program, it 
>>>>> bugs me a that I won't be able to run my code after I finish my 
>>>>> thesis without buying a license for the program(about $3.500 :-(( )
>>>>>
>>>>> So I've looked around for NL-stuff for R, but I can't find 
>>>>> anything. The closest thing appears to be optim(), but it doesn't 
>>>>> seem to allow constraints(as in fn = constant) or equations 
>>>>> systems. So, anyone knows if there is a method in R that you can 
>>>>> use for this purpose?
>>>>>
>>>>> regards
>>>>>
>>>>> ______________________________________________
>>>>> R-help at stat.math.ethz.ch mailing list
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide! 
>>>>> http://www.R-project.org/posting-guide.html
>>>>>
>>>>
>>>> ______________________________________________
>>>> R-help at stat.math.ethz.ch mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide! 
>>>> http://www.R-project.org/posting-guide.html
>>>>
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide! 
>>> http://www.R-project.org/posting-guide.html
>>
>>
>>
>>
>>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html




More information about the R-help mailing list