Может быть, вы можете добавить S в глобальную среду с помощью << -, а затем запустить цикл: </p>
Thermal<-function(S,t=0.000000000001,k=1){
#Defing beta
beta<-1/(k*t)
#multiplying each point of the array by all its adjacent points
#and summing them
Spointenergy<-S*((S2)+(S3)+S4+S5)
#Creating a loop over the whole array
for(i in 1:n){
for (j in 1:n){
#defining the change in energy at each point
dE<-energychange(S,i,j)
#By default each point does not flip
accept<-FALSE
#If energy decreases spin flip occurs 100% of the time
if (dE<0){
accept<-TRUE
}
#If energy increases a spin flip will occur if w is greater than or equal
#to u.
if (dE>=0){
w<-exp(-beta*dE)
u<-runif(1,0,1)
if (w>=u){
accept<-TRUE
}
}
#If no spin flip occurs there is no change in energy
if (accept==FALSE){
dE<-0
}
#if spin flip does occur then the magnitude of each lattice point is flipped
#from positive to negative or visa versa
if (accept==TRUE){
S[i,j]<--S[i,j]
}
}
}
return(S)
S<<-S
}
for (x in 1:10) {
Thermal(S)
}
S
Пример с набором данных радужной оболочки:
> iris_num <- iris[,-5]
> head(iris_num)
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 5.1 3.5 1.4 0.2
2 4.9 3.0 1.4 0.2
3 4.7 3.2 1.3 0.2
4 4.6 3.1 1.5 0.2
5 5.0 3.6 1.4 0.2
6 5.4 3.9 1.7 0.4
>
> testfunction <- function(iris_num)
+ {
+ iris_num <<- iris_num + 1
+ }
>
> testfunction(iris_num)
> head(iris_num)
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 6.1 4.5 2.4 1.2
2 5.9 4.0 2.4 1.2
3 5.7 4.2 2.3 1.2
4 5.6 4.1 2.5 1.2
5 6.0 4.6 2.4 1.2
6 6.4 4.9 2.7 1.4
>
> for (x in 1:10) {
+ testfunction(iris_num)
+ }
>
> head(iris_num)
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 16.1 14.5 12.4 11.2
2 15.9 14.0 12.4 11.2
3 15.7 14.2 12.3 11.2
4 15.6 14.1 12.5 11.2
5 16.0 14.6 12.4 11.2
6 16.4 14.9 12.7 11.4
>