2
使用R,我想知道迭代評估多個輸入和輸出函數的最佳方法。我通過看到的情節動機:http://paulbourke.net/fractals/clifford/使用2個輸入/輸出來加速迭代函數
關鍵方程爲:
x_{n+1} = sin(A* y_n) + C* cos(A* x_n)
y_{n+1} = sin(B* x_n) + D* cos(B* y_n)
我想存儲每個迭代的結果。我猜還有比通過下面的代碼描述的循環去一個更快的方法:
#Parameters
A <- -1.4
B <- 1.6
C <- 1.0
D <- 0.7
n_iter <- 10000000
#Initial values
x0 <- 0
y0 <- 0
#function to calculate n+1 points
cliff <- function(x,y){
c(sin(A*y) + C*cos(A*x), sin(B*x) + D*cos(B*y))
}
#matrix to store results
res_mat <- matrix(0,nrow=n_iter,ncol=2)
#recursive loop (definitely not the fastest way to do this?)
for (i in 2:n_iter){
res_mat[i,] <- cliff(res_mat[i-1,1],res_mat[i-1,2])
}
我想這實際上並不一定是一個單一的功能,但2,關於對方的輸出工作。任何洞察到更合適的方式來評估這些功能將不勝感激。我敢說我會從一些通用的編程建議中受益,而這些編程建議不一定是R特定的。