在R中的1000次迭代中未发现符号变化。

huangapple go评论95阅读模式
英文:

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 &lt;- function(x) { return((-1*sin(x)-3)^2+1 }
result &lt;- uniroot(f,c(0,5),extendInt = &quot;yes&quot;
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 }

在R中的1000次迭代中未发现符号变化。

英文:

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 &lt;- function(x) { (-1*sin(x) - 3)^2 + 1 }
m &lt;- 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 = &quot;red&quot;)

在R中的1000次迭代中未发现符号变化。

Also, the function f is identical to

g &lt;- function(x) { (sin(x) + 3)^2 + 1 }

答案3

得分: 0

For your purpose, you should use optim()optimize(), 而不是 uniroot(), 即:

给定 f &lt;- 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 &lt;- 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 = &quot;L-BFGS-B&quot;,lower = 0,upper = 5,control = list(fnscale = -1))

&gt; optim(0,f,method = &quot;L-BFGS-B&quot;,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] &quot;CONVERGENCE: REL_REDUCTION_OF_F &lt;= FACTR*EPSMCH&quot;

or

optimize(f, interval = c(0, 5), maximum = TRUE)

&gt; optimize(f, interval = c(0, 5), maximum = TRUE)
$maximum
[1] 1.570796

$objective
[1] -3

huangapple
  • 本文由 发表于 2020年1月3日 23:29:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定