布朗运动3D表示

时间:2018-04-11 17:28:44

标签: python graph 3d

到目前为止,我已经在1D和2D中使用了Brownian Motion的代码。我的3D图表显然不正确,因为我的x,y和z数据变量都是相同的。我只是不知道该怎么设置它们。我正在尝试关注https://www.mathworks.com/matlabcentral/fileexchange/32067-brownian-motion?focused=5191300&tab=function这是我的代码:

import numpy as np
from pylab import show
from math import sqrt
from scipy.stats import norm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D


def brownian(x0, n, dt, delta, out=None):

   #  n : The number of steps to take.
   #  dt : time step
   #  delta : "speed" of motion
   #  out :If `out` is NOT None, it specifies the array in which to put the
   #      result.  If `out` is None, a new numpy array is created and returned.
    x0 = np.asarray(x0) #I.C
    r = norm.rvs(size=x0.shape + (n,), scale=delta*sqrt(dt)) #generate n numbers for sample
    if out is None: #create out array
        out = np.empty(r.shape)
    np.cumsum(r, axis=-1, out=out) #cumulative sum for random variables
    out += np.expand_dims(x0, axis=-1)#initial condition.
    return out

def main():

    fig = plt.figure(1) #prepare plot
    ax1 = fig.add_subplot(231)
    ax2 = fig.add_subplot(232)
    ax = fig.add_subplot(2, 3, 3, projection='3d')

    delta = 2 # The Wiener process parameter.
    T = 10.0
    N = 500# Number of steps.
    dt = T/N
    m = 5 # Number of "lines"
    x = np.empty((m,N+1))# Create an empty array to store the realizations.
    x[:, 0] =  0# Initial values of x.

    brownian(x[:,0], N, dt, delta, out=x[:,1:]) 

    t = np.linspace(0.0, T, N+1)
    for i in range(m):
        ax1.plot(t, x[i])
    ax1.set_title('1D Brownian Motion')
    ax1.set_xlabel('t')
    ax1.set_ylabel('x')
    ax1.grid(True)

    ax2.plot(x[0],x[1])
    ax2.plot(x[0,0],x[1,0], 'go') #begin
    ax2.plot(x[0,-1], x[1,-1], 'ro') #end
    ax2.set_title('2D Brownian Motion')
    ax2.set_xlabel('x')
    ax2.set_ylabel('y')
    ax2.axis('equal')
    ax2.grid(True)

    #Data for a three-dimensional line
    zline = np.linspace(0, 15, 1000)
    xline = np.sin(zline)
    yline = np.cos(zline)
    ax.plot3D(xline, yline, zline, 'gray')

    # Data for three-dimensional scattered points
    zdata = brownian(x[:,0], N, dt, delta, out=x[:,1:]) 
    xdata = brownian(x[:,0], N, dt, delta, out=x[:,1:]) 
    ydata = brownian(x[:,0], N, dt, delta, out=x[:,1:]) 
    ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap='hot');


    ax.set_title('3D Brownian Motion')


    show()
    return
main()

1 个答案:

答案 0 :(得分:1)

第一次拨打brownian会产生5行,因为x[:, 0]的形状为(5,)

brownian(x[:,0], N, dt, delta, out=x[:,1:]) 

所以你可以用它们中的任何三个来产生3D布朗运动:

xdata, ydata, zdata = x[:3,:]
import numpy as np
from pylab import show
from math import sqrt
from scipy.stats import norm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D


def brownian(x0, n, dt, delta, out=None):

   #  n : The number of steps to take.
   #  dt : time step
   #  delta : "speed" of motion
   #  out :If `out` is NOT None, it specifies the array in which to put the
   #      result.  If `out` is None, a new numpy array is created and returned.
    x0 = np.asarray(x0) #I.C
    r = norm.rvs(size=x0.shape + (n,), scale=delta*sqrt(dt)) #generate n numbers for sample
    if out is None: #create out array
        out = np.empty(r.shape)
    np.cumsum(r, axis=-1, out=out) #cumulative sum for random variables
    out += np.expand_dims(x0, axis=-1)#initial condition.
    return out

def main():

    fig = plt.figure(1) #prepare plot
    ax1 = fig.add_subplot(231)
    ax2 = fig.add_subplot(232)
    ax = fig.add_subplot(2, 3, 3, projection='3d')

    delta = 2 # The Wiener process parameter.
    T = 10.0
    N = 500# Number of steps.
    dt = T/N
    m = 5 # Number of "lines"
    x = np.empty((m,N+1))# Create an empty array to store the realizations.
    x[:, 0] =  0# Initial values of x.

    brownian(x[:,0], N, dt, delta, out=x[:,1:]) 

    t = np.linspace(0.0, T, N+1)
    for i in range(m):
        ax1.plot(t, x[i])
    ax1.set_title('1D Brownian Motion')
    ax1.set_xlabel('t')
    ax1.set_ylabel('x')
    ax1.grid(True)

    ax2.plot(x[0],x[1])
    ax2.plot(x[0,0],x[1,0], 'go') #begin
    ax2.plot(x[0,-1], x[1,-1], 'ro') #end
    ax2.set_title('2D Brownian Motion')
    ax2.set_xlabel('x')
    ax2.set_ylabel('y')
    ax2.axis('equal')
    ax2.grid(True)

    xdata, ydata, zdata = x[:3,:]
    ax.plot3D(xdata, ydata, zdata)
    ax.set_title('3D Brownian Motion')


    show()
    return
main()

enter image description here