在R(pracma软件包)中正确使用fsolve的方法

在R(pracma软件包)中正确使用fsolve的方法,r,R,我正在尝试执行以下代码,以使用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

我正在尝试执行以下代码,以使用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)

寻找一组多项式的公共零点总是一件棘手的事情。不知怎的,我怀疑你例子中的多项式是否有这样一个精确的公共零点。无论如何,像fsolve中的那种实现会遇到梯度或步长太小的问题

更好的方法可能是应用最小二乘解算器,即最小化F分量的平方和。函数pracma::lsqnonlin将实现这一点,自动对F分量进行平方和求和

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
库(pracma)
x0
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
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