如何在R

时间:2019-05-29 14:45:19

标签: r optimization nonlinear-optimization

我想使用R中的以下代码来求解非线性方程组:

n <- 30
A <- B <- C <- list()
for (j in 1:10){
    A[[j]] <- matrix(rexp(300, rate=n), nrow=n, ncol=n)
    B[[j]] <- runif(n, min = 150, max = 30000)
    C[[j]] <- runif(n, min = 400, max = 60000)
}
alpha <- rep(0.1,n)
obj_fct <- function(alpha, A, B, C){
    Nt <- length(A)
    I <- E <- list()
    for (j in 1:Nt) {
        I[[j]] <- diag(dim(A[[j]])[1])
        E[[j]] <- solve(I[[j]]-alpha*A[[j]])
    }
    C_var <- B_var <- L_var <- E_var <- F_var <- list()
    for (j in 2:Nt) {
        C_var[[j]] <- C[[j]]-C[[j-1]]
        B_var[[j]] <- B[[j]]-B[[j-1]]
        E_var[[j]] <- E[[j]]-E[[j-1]]
        F_var[[j]] <- (1/2)*((E[[j-1]]+E[[j]])%*%B_var[[j]]) + (1/2)*(E_var[[j]]%*%(B[[j-1]]+B[[j]]))
    }
    Obj <- (unlist(F_var)-unlist(C_var))^2
    return(sum(Obj))
}

alpha_init <- rep(0.1,n)


res <- optim(par=alpha_init, fn=obj_fct, alpha=alpha, A=A, B=B, C= C, method="Nelder-Mead")

但是出现以下错误:

Error in fn(par, ...) : unused argument (par)

我遇到的主要问题是在代码中很好地定义要估计的参数,因为我有一个参数向量,我将其与矩阵乘以(哈达玛积)。

0 个答案:

没有答案