我有一个简短的问题,希望有人能帮我解决。我找不到解决办法。
我想使用uniroot()来查找函数的零点,或者更准确地说,是从函数的导数得到的。如果我把函数写成function(x)..这没有问题,但如果我想通过变量将派生函数带到uniroot,就不会有问题。
#This will not work:
deriv1 <- D(expression(x^2-2*x),"x")
uniroot(deriv1, c(0,5))
# This will work:
func <- function(x) 2 * x - 2
uniroot(func, c(0,5))
首先要感谢大家!
发布于 2021-09-22 09:14:07
您可以使用派生表达式作为主体来构造函数:
deriv1 <- D(expression(x^2-2*x),"x")
f <- function(x){}
body(f) <- deriv1
uniroot(f, c(0,5))
你也可以用符号演算代替uniroot
library(Ryacas)
fun <- yac_symbol("x^2-2*x")
dfun <- deriv(fun, "x")
solve(dfun, "x")
# {x==1}
并提取解决方案:
yac_solution <- solve(dfun, "x")
solution <- yac_symbol(paste0("x Where ", solution))
yac(solution)
https://stackoverflow.com/questions/69281268
复制相似问题