Untitled
unknown
plain_text
6 months ago
905 B
9
Indexable
import random import math import numpy as np N = 100 B = 20 steps = 0 particles = np.zeros(N) bin_size = 1 def move_particle(position): direction = random.choice([-1, 1]) new_position = position + direction if new_position < -B: new_position = -B elif new_position > B: new_position = B return new_position def compute_entropy(particles, B, bin_size): bins = np.arange(-B, B + bin_size, bin_size) counts, _ = np.histogram(particles, bins) probabilities = counts / N entropy = -np.sum([p * np.log2(p) for p in probabilities if p > 0]) return entropy while True: steps += 1 for i in range(N): particles[i] = move_particle(particles[i]) entropy = compute_entropy(particles, B, bin_size) print(f"Step: {steps}, Entropy: {entropy:.4f}") if np.all(particles == 0): print(f"Simulation completed in {steps} steps.") break
Editor is loading...
Leave a Comment