我试图在 R 中进行广泛的计算。十八个小时过去了,但我的 RStudio 似乎继续工作。我不确定是否可以以不同的方式编写脚本以使其更快。我试图在 50000 x 350 矩阵上实现Crank–Nicolson 类型的方法,如下所示:
#defining the discretization of cells
dt<-1
t<-50000
dz<-0.0075
z<-350*dz
#velocity & diffusion
v<-2/(24*60*60)
D<-0.02475/(24*60*60)
#make the big matrix (all filled with zeros)
m <- as.data.frame(matrix(0, t/dt+1, z/dz+2)) #extra columns/rows for boundary conditions
#fill the first and last columns with constant boundary values
m[,1]<-400
m[,length(m)]<-0
#implement the calculation
for(j in 2:(length(m[1,])-1)){
for(i in 2:length(m[[1]])){
m[i,][2:length(m)-1][[j]]<-m[i-1,][[j]]+
D*dt*(m[i-1,][[j+1]]-2*m[i-1,][[j]]+m[i-1,][[j-1]])/(dz^2)-
v*dt*(m[i-1,][[j+1]]-m[i-1,][[j-1]])/(2*dz)
}}
有没有办法知道 R 实现它需要多长时间?有没有更好的方法来构建数值计算?在这一点上,我觉得 excel 本来可以更快!