[R] can optimize solve paired euqations?
Ravi Varadhan
rvaradhan at jhmi.edu
Thu Dec 20 23:38:56 CET 2007
Hi Xin,
You can use the nlsolve() function that I have written to solve a nonlinear
system of equations. It converts a root finding problem into a minimization
problem, and uses optim() to find the minimizer.
A well-known problem with this approach to root-finding is that a local
minimum of the squared residual function is not necessarily a root of the
nonlinear system. A nice feature of nlsolve() is that it will try "really
hard" (by increasing the "nstarts" argument) to find the solution to the
system by identifying multiple local minima of the squared residual
function, if they exist.
Here is a solution to your problem (I rearranged your equations a bit).
func <- function(x, A, B, pz) {
f <- rep(NA,length(x))
f[1] <- A/(1-pz^10) - x[2]*(1-x[1])/(x[1]*(1-x[1]^x[2]))
f[2] <- A + B/A - 1/x[1] - x[2]*(1-x[1])/x[1]
f
}
> p0 <- c(0.2,10)
> set.seed(123)
> nlsolve(par=p0, fn=func, A=327.727, B=9517.336, pz=0.114^10, nstart=1000)
$par
[1] 0.03443476 11.68745035
$value
[1] 7.01806e-05
$counts
function gradient
148 15
$convergence
[1] 0
$message
NULL
Best,
Ravi.
----------------------------------------------------------------------------
-------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html
----------------------------------------------------------------------------
--------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Xin
Sent: Wednesday, December 19, 2007 5:12 PM
To: R-Help
Subject: [R] can optimize solve paired euqations?
I used the command below, but R gives me the error message--syntax error.
can anyone see the mistakes I made?
optimize(function(x,y)
+ ((327.727-(1-0.114^10)*y*(1-x)/x/(1-x^y))+(9517.336-327.727
*(1+(1-x)*(1+y)/x-327.727)))^2
+ interval=c(0,1))
At the same time, I use nlm() but R gives me the code
$code
[1] 3
function(vals) {
x <- vals[1]
y <- vals[2]
sum(c((199.458913633542-(1-0.114^10)*y*(1-x)/x/(1-x^y)),(5234.11964684527-19
9.458913633542*(1+(1-x)*(1+y)/x-199.458913633542)))^2)
}
nlm(f, c(2,2))
Any one know how to deal with this case?
Thanks
Xin
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: nlsolve_R.txt
Url: https://stat.ethz.ch/pipermail/r-help/attachments/20071220/90f06686/attachment.txt
More information about the R-help
mailing list