英文:
no sign change found in 1000 iterations in R
问题
Your code has a syntax error in R. Here's the corrected code:
f <- function(x) { return((-1*sin(x)-3)^2+1) }
result <- uniroot(f, c(0,5), extendInt = "yes")
result$root
result$f.root
The issue was a missing closing parenthesis in the f
function definition.
英文:
my question is "Find the maximum of the function myfun = - (sin(x)-3)^2 + 1 ,on the interval (0,5), and please answer x=? and f(x)= ?"
there is my code in R:
f <- function(x) { return((-1*sin(x)-3)^2+1 }
result <- uniroot(f,c(0,5),extendInt = "yes"
result$root
result$f.root
but the console is :
> Error in uniroot(f, c(0, 5), extendInt = "yes") :
no sign change found in 1000 iterations
what's wrong with my code
Thanks a lot
答案1
得分: 2
optimize
是用于寻找一维函数最大值或最小值的标准函数。uniroot
用于找到函数的根(0),而不是最大或最小值。
optimize(f, interval = c(0, 5), maximum = TRUE)
$maximum
[1] 1.570796
$objective
[1] 17
请参阅 ?optimize
获取示例和详细信息。
(注意:我在你的问题中添加了一个 )
到 f
,以避免语法错误。)
英文:
optimize
is the standard function for finding a max or min of a 1-dimensional function. uniroot
is used for finding a root (0) of the function, not max or min values.
optimize(f, interval = c(0, 5), maximum = TRUE)
$maximum
[1] 1.570796
$objective
[1] 17
See ?optimize
for examples and details.
(Note: I added a )
to the f
in your question to avoid syntax errors.)
答案2
得分: 1
The base R function being used in the question is wrong. From the documentation:
uniroot
> The function uniroot
searches the interval from lower to upper for a
> root (i.e., zero) of the function f with respect to its first
> argument.
optimize
> The function optimize
searches the interval from lower to upper for
> a minimum or maximum of the function f with respect to its first
> argument.
Code
f <- function(x) { (-1*sin(x) - 3)^2 + 1 }
m <- optimize(f, c(0, 5), maximum = TRUE)
m
#$maximum
#[1] 1.570796
#
#$objective
#[1] 17
curve(f, 0, 5)
points(m$maximum, m$objective, pch = 16, col = "red")
Also, the function f
is identical to
g <- function(x) { (sin(x) + 3)^2 + 1 }
英文:
The base R function being used in the question is wrong. From the documentation:
uniroot
> The function uniroot
searches the interval from lower to upper for a
> root (i.e., zero) of the function f with respect to its first
> argument.
optimize
> The function optimize
searches the interval from lower to upper for
> a minimum or maximum of the function f with respect to its first
> argument.
Code
f <- function(x) { (-1*sin(x) - 3)^2 + 1 }
m <- optimize(f, c(0, 5), maximum = TRUE)
m
#$maximum
#[1] 1.570796
#
#$objective
#[1] 17
curve(f, 0, 5)
points(m$maximum, m$objective, pch = 16, col = "red")
Also, the function f
is identical to
g <- function(x) { (sin(x) + 3)^2 + 1 }
答案3
得分: 0
For your purpose, you should use optim()
或 optimize()
, 而不是 uniroot()
, 即:
给定 f <- function(x) -(sin(x)-3)^2+1
(你的代码中的目标函数与你在帖子开头描述的不同),你可以通过以下方式获得结果:
optim(0, f, method = "L-BFGS-B", lower = 0, upper = 5, control = list(fnscale = -1))
或者
optimize(f, interval = c(0, 5), maximum = TRUE)
英文:
For you purpose, you should use optim()
or optimize()
, instead of uniroot()
, i.e.:
Given f <- function(x) -(sin(x)-3)^2+1
(the objective function in you code is not the one as you described at the beginning of your post), you will get the result via
optim(0,f,method = "L-BFGS-B",lower = 0,upper = 5,control = list(fnscale = -1))
> optim(0,f,method = "L-BFGS-B",lower = 0,upper = 5,control = list(fnscale = -1))
$par
[1] 1.570796
$value
[1] -3
$counts
function gradient
7 7
$convergence
[1] 0
$message
[1] "CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH"
or
optimize(f, interval = c(0, 5), maximum = TRUE)
> optimize(f, interval = c(0, 5), maximum = TRUE)
$maximum
[1] 1.570796
$objective
[1] -3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论