#!My question requires me to find 2 local maximum for y using this equation:
x <-seq(-5,5,length =10001)
y<-(10 *((x-1)^2 )^(1/3))/(x^2+9)
plot(x,y)
我相信我可以通过max(y)或y [which.max(y)]获得一个最大值。 但是我找不到其他最大值,因为该图有2个峰值。
y[which.max(y)]
#> [1] 1.637347
max(y)
#> [1] 1.637347
我尝试用这种方法找到第二个局部最大值,但不确定是否正确
y[which.max(x>2)]
#> [1] 0.7695067
由reprex package(v0.2.0)于2019-03-24创建。
答案 0 :(得分:0)
这是完成任务的一种round回方式(正确吗?)。
#Library kader for cuberoot() function
library('kader')
#Declaring x and calculating y
varX <- seq(-5, 5, length = 10001)
crootX <- kader:::cuberoot((varX-1)^2)
actY <- (10*crootX)/(varX^2+9)
#Storage variable for the maxima
outs <- c()
#Looping through y values looking for those values that are greater than both the preceding AND succeeding values
for(i in 1:length(actY)){
if(actY[i] > actY[i-1] && actY[i] > actY[i+1]){
outs[i] <- actY[i]
}
}
#Subsetting said values and output
outs <- subset(outs, !is.na(outs))
outs
#[1] 1.6373473 0.8818895