Solve system of non-linear equations

ε祈祈猫儿з 提交于 2021-02-10 10:54:30

问题


I am trying to solve the following system of four equations. I have tried using the "rootSolve" package but it does not seem like I can find a solution this way.

The code I am using is the following:

model <- function(x) {
F1 <- sqrt(x[1]^2 + x[3]^2) -1
F2 <- sqrt(x[2]^2 + x[4]^2) -1
F3 <- x[1]*x[2] + x[3]*x[4]
F4 <- -0.58*x[2] - 0.19*x[3]
c(F1 = F1, F2 = F2, F3 = F3, F4 = F4)
}
(ss <- multiroot(f = model, start = c(0,0,0,0)))

But it gives me the following error:

Warning messages:
1: In stode(y, times, func, parms = parms, ...) :
error during factorisation of matrix (dgefa);         singular matrix
2: In stode(y, times, func, parms = parms, ...) : steady-state not reached

I have changed the starting values, as suggested in another similar answer, and for some I can find a solution. However, this system - according to the source I am using - should have an uniquely identified solution. Any idea about how to solve this system?

Thanks you!


回答1:


Your system of equations has multiple solutions. I use a different package to solve your system: nleqslv as follows:

library(nleqslv)

model <- function(x) {
   F1 <- sqrt(x[1]^2 + x[3]^2) - 1
   F2 <- sqrt(x[2]^2 + x[4]^2) - 1
   F3 <- x[1]*x[2] + x[3]*x[4]
   F4 <- -0.58*x[2] - 0.19*x[3]
   c(F1 = F1, F2 = F2, F3 = F3, F4 = F4)
}

#find solution
xstart  <-  c(1.5, 0, 0.5, 0)
nleqslv(xstart,model)

This gets the same solution as the answer of Prem.

Your system however has multiple solutions. Package nleqslv provides a function to search for solutions given a matrix of different starting values. You can use this

set.seed(13)
xstart <- matrix(runif(400,0,2),ncol=4)
searchZeros(xstart,model)

(Note: different seeds may not find all four solutions)

You will see that there are four different solutions:

$x
     [,1]          [,2]          [,3] [,4]
[1,]   -1 -1.869055e-10  5.705536e-10   -1
[2,]   -1  4.992198e-13 -1.523934e-12    1
[3,]    1 -1.691309e-10  5.162942e-10   -1
[4,]    1  1.791944e-09 -5.470144e-09    1
.......

This clearly suggests that the exact solutions are as given in the following matrix

xsol <- matrix(c(1,0,0,1,
                 1,0,0,-1,
                -1,0,0,1,
                -1,0,0,-1),byrow=TRUE,ncol=4)

And then do

model(xsol[1,])
model(xsol[2,])
model(xsol[3,])
model(xsol[4,])

Confirmed! I have not tried to find these solutions analytically but you can see that if x[2] and x[3] are zero then F3 and F4 are zero. The solutions for x[1] and x[4] can then be immediately found.




回答2:


Above warning states that using a starting value which you provided to multiroot it couldn't find an optimal solution.

Let's try this -

library(rootSolve)

model <- function(x) {
  F1 <- sqrt(x[1]^2 + x[3]^2) - 1
  F2 <- sqrt(x[2]^2 + x[4]^2) - 1
  F3 <- x[1]*x[2] + x[3]*x[4]
  F4 <- -0.58*x[2] - 0.19*x[3]
  c(F1 = F1, F2 = F2, F3 = F3, F4 = F4)
  }

#solution
(ss <- multiroot(f = model, start = c(1.5, 0, 0.5, 0)))

it gives

> ss
$root
[1]  1.000000e+00  4.752703e-12 -1.450825e-11  1.000000e+00

$f.root
           F1            F2            F3            F4 
 3.404610e-12  3.494982e-13 -9.755549e-12  1.929753e-20 

$iter
[1] 7

$estim.precis
[1] 3.377414e-12

After multiple trial I observed that whenever I change it's starting value I get almost the same result (i.e. 1, 0, 0, 1) every time.



来源:https://stackoverflow.com/questions/51191547/solve-system-of-non-linear-equations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!