3D布朗运动表示异常求助:已实现1D、2D布朗运动代码
Hey there, let's fix that 3D Brownian Motion issue you're running into! The problem here is almost certainly that you're using the same random increment data for all three axes—Brownian motion in 3D requires three independent sets of normally distributed increments for x, y, and z respectively.
You mentioned that x, y, z are identical, which tells me you probably reused the same array for all three dimensions. Let's break down how to generate proper independent 3D Brownian motion, building on your existing code structure.
Corrected Code Implementation
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from math import sqrt def generate_3d_brownian(n_steps, dt=0.01): # Generate independent normal increments for each axis # Each increment has mean 0, standard deviation sqrt(dt) dx = np.random.normal(0, sqrt(dt), n_steps) dy = np.random.normal(0, sqrt(dt), n_steps) dz = np.random.normal(0, sqrt(dt), n_steps) # Compute cumulative sum to build the trajectory over time x = np.cumsum(dx) y = np.cumsum(dy) z = np.cumsum(dz) return x, y, z # Set simulation parameters total_steps = 1000 time_step = 0.01 # Generate 3D Brownian motion data x_pos, y_pos, z_pos = generate_3d_brownian(total_steps, time_step) # Plot the 3D trajectory fig = plt.figure(figsize=(10, 8)) ax = fig.add_subplot(111, projection='3d') ax.plot(x_pos, y_pos, z_pos, linewidth=1.2) # Add labels and title for clarity ax.set_xlabel('X Position') ax.set_ylabel('Y Position') ax.set_zlabel('Z Position') ax.set_title('3D Brownian Motion Trajectory') plt.show()
Key Fixes & Context
- Independent Random Increments: The core issue was likely reusing the same increment array for all three axes (e.g.,
y = xordz = dx). By generatingdx,dy,dzas separate normal distributions, we ensure each axis moves independently—this is the defining feature of 3D Brownian motion. - Variance Scaling: Using
sqrt(dt)as the standard deviation ensures the motion's variance grows linearly with time, which matches the mathematical properties of Brownian motion. - Clean Trajectory Calculation:
np.cumsum()efficiently turns small, random steps into a continuous path, just like you'd do for 1D or 2D motion.
If you were referencing a MATLAB implementation, the logic translates directly—you just need to make sure each dimension gets its own set of random numbers instead of reusing the same sequence.
内容的提问来源于stack exchange,提问作者undergrad

