我怎样才能最小化这个功能? [英] How can I minimize this function?

查看:53
本文介绍了我怎样才能最小化这个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图证明那里某个函数不能为负.由于我没有设法证明,也没有说服自己这是真的,所以我对函数进行了如下编码:

I was trying to prove there that a certain function cannot go negative. As I did not manage to make the proof, and also to convince myself that it is true, I coded the function as follows:

test = function(s,t){
  # s is a vector of positives reals of length d
  # t is a vector of complexes of length d-1
  d = length(s)
  t = c(t, (1-sum(t*s[1:(d-1)]))/s[d])
  modulii = abs((t+1)/(t-1))
  return(max(modulii)-1)
}

# I want to minimize this test function over all 
  # s positive reals of length d
  # t complex of length d-1. 

# How can i do that ? 

# simple starting points: 
d = 3
s = runif(d)
t = complex(real = runif(d-1),imaginary = runif(d-1))

test(s,t) # should be positive.

我如何编写一个优化程序来最小化这个函数:

How can I code an optimization routine that minimize this function with respect to :

  • s[1],...s[d] 所有非负实数,s[d] 严格为正.
  • t[1],...,t[d-1] 所有复数值.
  • s[1],...s[d] all non-negative reals, with s[d] strictly positive.
  • t[1],...,t[d-1] all complex valued.

?

我在 optim 和复数上挣扎.我想确保最小值不能为负 ;)

I struggle with optim and complex numbers. I want to be sure that the minimum cannot be negative ;)

推荐答案

定义一个函数 proj,它采用长度为 3*d-2 的向量,并从中生成一个包含 s 和 t 的列表,将前 d 个元素平方形成 s并将接下来的 d-1 和后面的 d-1 个元素作为 t 的实部和虚部.然后定义 f 运行 proj 并将结果传递给 test 和 run.

Define a function proj which takes a vector of length 3*d-2 and produces a list with s and t from it squaring the first d elements to form s and taking the next d-1 and following d-1 elements as the real and imaginary parts of t. Then define f to run proj and pass the result to test and run.

d <- 3
proj <- function(x) {
  d <- (length(x) + 2) / 3
  list(s = head(x, d)^2, t = complex(x[seq(d+1, length = d-1)], tail(x, d-1)))
}   
f <- function(x) with(proj(x), test(s, t))
result <- optim(rep(0.5, 3*d-2), f)
result
## $par
## [1]  1.0863555573  5.9011341467 -0.0009866435 -0.1252050359  1.0720624611
## [6] -0.3826544395 -6.2322265938
##
## $value
## [1] 8.911303e-09
##
## $counts
## function gradient 
##      188       NA 
##
## $convergence
## [1] 0
##
## $message
## NULL

proj(result$par)
## $s
## [1] 1.180168e+00 3.482338e+01 9.734655e-07
##
## $t
## [1] -0.3826544+0i -6.2322266+0i

这篇关于我怎样才能最小化这个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆