# Non-linear system of equations

10 messages
Open this post in threaded view
|

## Non-linear system of equations

 Hello R users, I am trying to estimate the parameters of a bimodal normal distribution using moments matching, so I have to solve a non-linear system of equations. How can I solve the following simple example? x^2 - y^2 = 6 x – y = 3 I heard about nlsystemfit, but I don’t know how to run it exactly. I have tried the following code, but it doesn’t really work: f1 <-y~ x[1]^2-x[2]^2-6 f2 <-z~ x[1]-x[2]-3 f  <- list(f1=0,f2=0) nlsystemfit("OLS",f,startvals=c(0,0)) Thank You in advance for your help. Evgeniq Petrova ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Non-linear system of equations

 2008/4/25 Radka Pancheva <[hidden email]>: >  I am trying to estimate the parameters of a bimodal normal distribution using moments matching, so I have to solve a non-linear system of equations. How can I solve the following simple example? > >  x^2 - y^2 = 6 >  x – y = 3 > >  I heard about nlsystemfit, but I don't know how to run it exactly. I have tried the following code, but it doesn't really work: > > >  f1 <-y~ x[1]^2-x[2]^2-6 >  f2 <-z~ x[1]-x[2]-3 >  f  <- list(f1=0,f2=0) >  nlsystemfit("OLS",f,startvals=c(0,0)) You could try the recent package BB by Ravi Varadhan. The code could be the following: library(BB) f <- function(x) {   x1 <- x[1]   x2 <- x[2]   F <- rep(NA, 2)   F[1] <- x1^2 - x2^2 - 6   F[2] <- x1 - x2 - 3   return(F) } p0 <- c(1,2) dfsane(par=p0, fn=f,control=list(maxit=3000)) I got the solution: x1 = 2.5 x2 = -0.5 Paul ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Non-linear system of equations

Open this post in threaded view
|

## Re: Non-linear system of equations

 In reply to this post by Radka Pancheva 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
Open this post in threaded view
|

## Re: Non-linear system of equations

Open this post in threaded view
|

## Re: Non-linear system of equations

Open this post in threaded view
|

## Re: Non-linear system of equations

Open this post in threaded view
|