'How to solve nonlinear systems of equations in R when the equations contain summation?

the equation I need to solve

n and each x_i is known, unknowns are \lambda and \mu.

my code like this:

library(BB)
n=100
l=0
s=1
sample=rcauchy(n,l,s)
fun<-function(x){
  f<-numeric(length(x))
  sum1=0
  sum2=0
  for (i in 1:n){
    sum1=sum1+sample[i]/(x[2]^2+(sample[i]-x[1])^2)
    sum2=sum2+1/(x[2]^2+(sample[i]-x[1])^2)
  }
  f[1]<-sum1-x[1]*n/(2*x[2]^2)
  f[2]<-sum2-n/(2*x[2]^2)
  f
}
startx<-c(0,0)
result = dfsane(startx,fun,control=list(maxit=500,trace = FALSE))
theta=result$par

the error is "Failure in initial functional evaluation"

r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source