使用二维阵列构建耦合贴图晶格

2024-10-01 02:30:27 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想在我的计算机上建立一个耦合的映射晶格

耦合映射晶格(CML)由以下等式给出:

The equation

其中,函数f(Xn)是一个逻辑图:

The function

对于该CML,x值为0-1,r=4

注意:“n”可以被认为是时间,“i”可以被认为是空间

我花了很多时间来理解迭代,并提出了如下代码,但是我不确定这是否是迭代这个等式的正确代码

注意:我使用了2d numpy数组,其中行是'n',列是'I',从代码中可以明显看出

基本上,我想开发一个代码来模拟这个等式,下面是我的看法

不要直接跳转到代码,如果不费心先看一下方程式,就无法理解发生了什么。

import numpy as np
import matplotlib.pyplot as plt

'''The 4 definitions created below are actually similar and only vary in their indexings. These 4 
   have been created only because of the if conditions I have put in the for loop '''
def logInit(r,x):
    y[n,0]=r*x[n,0]*(1-x[n,0])
    return y[n,0]

def logPresent(r,x):
    y[n,i]=r*x[n,i]*(1-x[n,i])
    return y[n,i] 

def logLast(r,x):
    y[n,L-1]=r*x[n,L-1]*(1-x[n,L-1])
    return y[n,L-1] 

def logNext(r,x):
    y[n,i+1]=r*x[n,i+1]*(1-x[n,i+1])
    return y[n,i+1]

def logPrev(r,x):
    y[n,i-1]=r*x[n,i-1]*(1-x[n,i-1])
    return y[n,i-1]

# 2d array with 4 row, 3 col. I created this because I want to store the evaluated values of log 
  function into this y[n,i] array

y=np.ones(12).reshape(4,3)

# creating an array of random numbers between 0-1 with 4 rows 3 columns  

np.random.seed(0)
x=np.random.random((4,3))

L=3
r=4
eps=0.5

for n in range(3):
    for i in range(L):


        if i==0:
            x[n+1,i]=(1-eps)*logPresent(r,x) + 0.5*eps*(logLast(r,x)+logNext(r,x))

        elif i==L-1:
              x[n+1,i]=(1-eps)*logPresent(r,x) + 0.5*eps*(logPrev(r,x) + logInit(r,x))

        elif i > 0 and i < L - 1:
             x[n+1,i]=(1-eps)*logPresent(r,x) + 0.5*eps*(logPrev(r,x) +logNext(r,x))

        print(x)

这确实给出了一个输出。这是:

[[0.5488135  0.71518937 0.60276338]
 [0.94538775 0.82547604 0.64589411]
 [0.43758721 0.891773   0.96366276]
 [0.38344152 0.79172504 0.52889492]]

[[0.5488135  0.71518937 0.60276338]
 [0.94538775 0.82547604 0.92306303]
 [0.2449672  0.49731638 0.96366276]
 [0.38344152 0.79172504 0.52889492]]

[[0.5488135  0.71518937 0.60276338]
 [0.94538775 0.82547604 0.92306303]
 [0.2449672  0.49731638 0.29789622]
 [0.75613708 0.93368134 0.52889492]]  

但我很确定这不是我想要的

如果你能想出一个正确的方法,用代码迭代和循环CML方程?建议我必须做的改变。非常感谢

您必须考虑模拟该等式所需的迭代和循环。这可能会很乏味,但这是唯一可以建议我修改代码的方法。


Tags: ofthe代码inforreturndefnp
1条回答
网友
1楼 · 发布于 2024-10-01 02:30:27

我觉得你的计算很好。通过沿空间维度使用矢量化和重用中间结果y,可以提高速度。我对你的程序做了一点调整,但本质上和以前一样。对我来说,结果似乎是可信的。图像显示了第一行中的随机初始向量,随着时间的推移(从上到下),耦合开始发挥作用,形成小岛和图案

import numpy as np
import matplotlib.pyplot as plt

L = 128  # grid size
N = 128  # time steps 
r = 4
eps = 0.5

# Create random values for the initial time step
np.random.seed(0)
x = np.zeros((N+1, L))
x[0, :] = np.random.random(L)

# Create a helper matrix to save and reuse part of the calculations
y = np.zeros((N, L))

# Indices for previous, present, next position for every point on the grid
idx_present = np.arange(L)        #   0, 1, ..., L-2, L-1
idx_next = (idx_present + 1) % L  #   1, 2, ..., L-1, 0
idx_prev = (idx_present - 1) % L  # L-1, 0, ..., L-3, L-2

def log_vector(rr, xx):
    return rr * xx * (1 - xx)

# Loop over the time steps
for n in range(N):
    # Compute y once for the whole time step and reuse it 
    # to build the next time step with coupling the neighbours
    y[n, :] = log_vector(rr=r, xx=x[n, :])
    x[n+1, :] = (1-eps)*y[n,idx_present] + 0.5*eps*(y[n,idx_prev]+y[n,idx_next])

# Plot the results
plt.imshow(x)

resulting grid with N=L=128, r=4, eps=0.5

相关问题 更多 >