Re: [R] Non-linear system of equations

From: Berend Hasselman <>
Date: Fri, 25 Apr 2008 05:55:14 -0700 (PDT)

> 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     


# 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



Sometimes minpack.lm can be used to find roots. Do


and in this case you'll see that it doesn't work.

In this case stick to nlm and/or nlminb.


View this message in context:
Sent from the R help mailing list archive at

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 25 Apr 2008 - 13:06:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 25 Apr 2008 - 13:30:31 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive