[R] Non-linear system of equations
Berend Hasselman
bhh at xs4all.nl
Fri Apr 25 14:55:14 CEST 2008
> x^2 - y^2 = 6
> x – y = 3
>
>
You can also try this
# function
f <- function(x) {
y <- numeric(2)
y[1] <- x[1]^2-x[2]^2-6
y[2] <- x[1]-x[2]-3
y
}
# function values transformed to scalar
# minimising fnorm this way is not the best method of finding a solution for
f(x)=0
# there may be values for x which minimise fnorm but do not set f(x) = 0
# but you can always try
fnorm <- function(z) {p <- f(z);return(crossprod(p))}
#starting values
xstart <- c(0,0)
# You can use nlm or nlminb
nlm(fnorm,xstart)
nlminb(xstart,fnorm)
Sometimes minpack.lm can be used to find roots.
Do
library(minpack.lm)
nls.lm(xstart,f)
and in this case you'll see that it doesn't work.
In this case stick to nlm and/or nlminb.
Berend
--
View this message in context: http://www.nabble.com/Non-linear-system-of-equations-tp16893056p16895541.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list