Reputation: 21
I need some help solving these very silly six nonlinear equations using R.
sigma1^2 = b1^2 + b2^2
sigma2 = b1* b3 + b2*b4
sigma3^2 = b3^2 + b4^2
sigma4^2 = lambda1 * b1^2 + lambda2 * b2^2
sigma5 = lambda1 * b1 * b3 + lambda2 * b2 * b4
sigma6^2 = lambda1 * b3^2 + lambda2 * b4^2
I already know all the sigma values, now I need to know b and lambda values. What I already tried is:
library(nleqslv)
fun <- function(x) {
f <- c(sigma1[1,1], sigma1[1,2], sigma1[2,2], sigma2[1,1], sigma2[1,2], sigma2[2,2])
f[1] <- x[1]^2 + x[2]^2
f[2] <- x[1]*x[3]+ x[2]*x[4]
f[3] <- x[3]^2 + x[4]^2
f[4] <- x[5]*x[1]^2+x[6]*x[2]^2
f[5] <- x[5]*x[1]*x[3] + x[6]*x[2]*x[4]
f[6] <- x[5]*x[3]^2 + x[6]*x[4]^2
return(f)
}
startx <- c(0.01,0.02,0.02,0.01,1.5,1.7)
nleqslv(startx,fun,jacobian=TRUE,control=list(btol=.01))
Where x[1] till x[4] are my b values, x[5] and x[6] are my lambda values.
The problem is the returned $fvec values are inconsistent with the sigma values. What am I doing wrong? Would be great if you could share your opinions, thank you!
Upvotes: 1
Views: 44
Reputation: 21
Ok, I just tried the following and it worked, hope it can be helpful for somebody else with the same problem:
ini <- function(x){
c(x[1]^2 + x[2]^2 - sigma1[1,1],
x[1]*x[3]+ x[2]*x[4] - sigma1[1,2],
x[3]^2 + x[4]^2 - sigma1[2,2],
x[5]*x[1]^2+x[6]*x[2]^2 - sigma2[1,1],
x[5]*x[1]*x[3] + x[6]*x[2]*x[4] - sigma2[1,2],
x[5]*x[3]^2 + x[6]*x[4]^2 - sigma2[2,2])
}
x0 <- c(0.01,0.02,0.02,0.01,1.5,1.7)
nleqslv(x0, ini, method = "Broyden")
The initial values in X0 were really selected arbitrarily, but the final results of x coincide with sigma values after they are transformed according to the equations. I'm very happy now :D
Upvotes: 1