我正在尝试使用R:
执行以下代码来求解非线性方程组library(pracma)
t <- read.csv("values-try.csv", header=F, sep=",")
x0 <- as.matrix( c(0, 1, 0, 1, 0, 1))
Gr <- 9.807
F <- function (x) {
x1 <- x[1]; x2 <- x[2]; x3 <- x[3]; x4 <- x[4]; x5 <- x[5]; x6 <- x[6]
as.matrix( c( (t[1,1] - x1)^2/x2^2 + (t[1,2] - x3)^2/x4^2 + (t[1,3] - x5)^2/x6^2 - Gr^2,
(t[2,1] - x1)^2/x2^2 + (t[2,2] - x3)^2/x4^2 + (t[2,3] - x5)^2/x6^2 - Gr^2,
(t[3,1] - x1)^2/x2^2 + (t[3,2] - x3)^2/x4^2 + (t[3,3] - x5)^2/x6^2 - Gr^2,
(t[4,1] - x1)^2/x2^2 + (t[4,2] - x3)^2/x4^2 + (t[4,3] - x5)^2/x6^2 - Gr^2,
(t[5,1] - x1)^2/x2^2 + (t[5,2] - x3)^2/x4^2 + (t[5,3] - x5)^2/x6^2 - Gr^2,
(t[6,1] - x1)^2/x2^2 + (t[6,2] - x3)^2/x4^2 + (t[6,3] - x5)^2/x6^2 - Gr^2), ncol = 1)
}
fsolve(F, x0)
我一直收到以下错误:
Error in if (norm(s, "F") < tol || norm(as.matrix(ynew), "F") < tol) break :
missing value where TRUE/FALSE needed
Calls: fsolve -> broyden
Execution halted
任何提示或解决错误?
values-try.csv如下所示:
0.1191419256974832, -0.2806359683994824, -9.755712465258934
0.3194200198415491, 0.05681698915395282, -9.711375649078391
0.05320046522270569, 0.21071993729858585, -9.711942750423542
0.056291795600583824, 0.20746318577998762, -9.697096562782926
-0.18870002789891743, -0.03873042128470452, -9.70831243701548
0.13239301222057243, -9.790554976542873, -0.9744148062871234
答案 0 :(得分:0)
Finding the common zeroes of a set of polynomials is always tricky business. I somehow doubt the polynomials in your example do have such an exact common zero. Anyway, implementations like the one in fsolve will have problems with too small gradients or step lengths.
A better idea might be to apply a least-squares solver, i.e., minimize the sum of squares of the components of F. Function pracma::lsqnonlin will do this, squaring and summing the components of F automatically.
library(pracma)
x0 <- as.matrix( c(0, 1, 0, 1, 0, 1))
sol = lsqnonlin(F, x0, options=list(tolx=1e-12, tolg=1e-12))
sol$x
## [1] 0.1061871 32.9875053 -0.5361180
## [4] 59.1224428 68975.6833271 7034.3066917
F(sol$x)
## [,1]
## [1,] 1.838934e-07
## [2,] 9.420962e-08
## [3,] 2.146091e-05
## [4,] -2.161610e-05
## [5,] -1.225254e-07
## [6,] -3.836504e-10
Please note that you will find other minima with different starting points. You didn’t say whether you want to restrict the problem domain; I am quite certain there are no ‘near’ zeros in [-10, 10]^6.