# [R] solving system of nonlinear equations

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Nov 20 18:42:07 CET 2004

```On Sat, 20 Nov 2004, Enayetur RAHEEM wrote:

> R version: 2.0.0
> OS: WinXP, SP2
>
> I am using "nls" to estimate parameters of a system of nonlinear
> equations. Although, iteration is not converging, I would like to get
> the final estimates and store them in the object, say, "RR".

Please read the help page for nls: this is a misuse of the function:

*Do not use 'nls' on artificial "zero-residual" data.*

You could do this, easily, using a general-purpose optimizer such as
optim().

> Any help would be appreciated. Thanks.
>
> The following is not working ( I mean, I can not store it in "RR")
>
> RR=nls(k~exp(-(q1-lam)/sig)+exp(-(q2-del)/tau),start=st,data=d1,trace=T,control=ctrl)
> 13.24433 :  30 30 15 10
> 3.90071 :  33.00603 38.86974 20.18227 10.15656
> 0.02326074 :  31.44802 36.80447 20.01395 10.07528
> 1.209036e-06 :  31.39755 36.58548 19.99954 10.05675
> 1.543785e-15 :  31.39752 36.58347 19.99947 10.05640
> 1.030527e-29 :  31.39752 36.58347 19.99947 10.05640
> 8.019572e-31 :  31.39752 36.58347 19.99947 10.05640
> 8.019572e-31 :  31.39752 36.58347 19.99947 10.05640
> 8.019572e-31 :  31.39752 36.58347 19.99947 10.05640
> 8.019572e-31 :  31.39752 36.58347 19.99947 10.05640
> 8.019572e-31 :  31.39752 36.58347 19.99947 10.05640
> Error in nls(k ~ exp(-(q1 - lam)/sig) + exp(-(q2 - del)/tau), start = st,  :
>        number of iterations exceeded maximum of 10
>
>
>
> On Sun, 14 Nov 2004 17:09:06 -0800, Spencer Graves
> <spencer.graves at pdf.com> wrote:
>>       Have you considered "nls"?  If you read the help file and work
>> through the examples, there is a good chance you can make it work, I
>> think.  I think I would start trying "plinear" in "nls", parameterizing
>> the problem in terms of alpha, beta, ln.sigma, and ln.tau, unless you
>> think a solution might require sigma < 0 or tau < 0.  Using logarithms
>> will get rid of the constraint and may make the problem numerically
>> easier.  Using alpha and beta rather than lambda and delta transforms
>> the problem into an ordinary least squares problem for alpha and beta
>> given any two numbers for sigma and tau (or ln.sigma and ln.tau).
>>
>>       If I had trouble with this, I might try two other things:
>>
>>       (a) The "solver" in Excel.
>>
>>       (b) I might generate a grid in ln.sigma and ln.tau using
>> expand.grid.  For each combination of levels, I'd set up the linear
>> regression problem and use "lm" to estimate alpha and beta and compute
>> and store the sum of squares of residuals.  Then I'd use "contour" to
>> visualize the sum of squares surface.
>>
>>       I've done all these things with crudely similar problems in the
>> past and been happy with the results.  If I only had this one problem,
>> I'd be surprised if it would require more than a few hours.  If I wanted
>> a general algorithm for other purposes, I might do it two or three
>> different ways both to help select a good algorithm and to build
>> confidence in the results.
>>
>>       hope this helps.
>>       spencer graves
>> p.s.  Some of these techniques are discussed in Venables and Ripley
>> (2002) Modern Applied Statistics with S, 4th ed. (Springer).  If you
>> don't have this, I'd encourage you to consider spending some time with it.
>>
>>
>>
>> Enayetur RAHEEM wrote:
>>
>>> Hello there
>>>
>>> Can anybody please tell me if there is any package in R to solve the
>>> following 4 nonlinear equations with 4 unknowns:
>>>
>>> alpha*exp(20/sigma)+ beta*exp(21/tau) = 2
>>> alpha*exp(22/sigma)+ beta*exp(9/tau) = 4
>>> alpha*exp(10/sigma)+ beta*exp(30/tau) = 6
>>> alpha*exp(40/sigma)+ beta*exp(39/tau) = 5
>>>
>>> where
>>>
>>> alpha = exp(lambda/sigma)
>>> beta= exp(delta/tau)
>>>
>>> I need to estimate lambda, sigma, delta, tau
>>>
>>> Thanks.
>>> E Raheem
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>
>>>
>>
>> --
>> Spencer Graves, PhD, Senior Development Engineer
>> O:  (408)938-4420;  mobile:  (408)655-4567
>>
>>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
>
>

--
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

```