gpt4 book ai didi

r - R 中使用 persp3d 图实现 Ackley 函数

转载 作者:行者123 更新时间:2023-11-30 09:08:13 25 4
gpt4 key购买 nike

x1<-as.matrix(seq(-32.768,32.768,length=100))

x2<-as.matrix(seq(-32.768,32.768,length=100))

X<-cbind(x1,x2)

y <- outer(X,X,Ackley)

功能实现

Ackley <- function(x1,x2){
a<-20
b<- 0.2
c<-(2*pi)

fofx1<- -a*exp(-b*sqrt((rowSums(X^2)/100)))-
exp(rowSums(cos((c*X)/100)))+a+exp(1)

fofx2 <- -a*exp(-b*sqrt((rowSums(X^2)/100)))-
exp(rowSums(cos((c*X)/100)))+a+exp(1)
return(fofx1+fofx2)
}

我收到这样的错误 -dim(robj) <- c(dX, dY) 中的错误: dims [产品 40000] 与对象的长度 [100] 不匹配

我不会这样绘制 Ackley function plot

最佳答案

ackley <- function(x1, x2) {
a <- 20
b <- 0.2
c <- (2*pi)
d <- 2
fofx1 <- -a*exp(-b*sqrt(1/d*sum(c(x1,x2)^2))) -
exp(sum( cos(c*c(x1,x2))/d))+a+exp(1)

#fofx2 <- -a*exp(-b*sqrt(sum(c(x1,x2)^2)/100))-
# exp(sum( cos(c*c(x1,x2))/100))+a+exp(1)
return(fofx1)
}
Ackley <- Vectorize(ackley)

x1 <- seq(-32.768,32.768,length=500)
x2 <- seq(-32.768,32.768,length=500)
z <- outer(x1, x2, FUN="Ackley")

library(plotly)
plot_ly(x=~x1, y=~x2, z = ~z, type="surface") %>%
layout(scene=list(aspectratio = list(x = 1, y = 1, z = 1)))

enter image description here

关于r - R 中使用 persp3d 图实现 Ackley 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46988881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com