How to draw a solid of revolution of a polynomial function around the y-axis?

前端 未结 1 1873
旧时难觅i
旧时难觅i 2020-12-20 08:37

A solid of revolution of a function y = x**2 around the y-axis can be plotted using the code below:

import numpy as np
import matplotlib.pyplot          


        
相关标签:
1条回答
  • 2020-12-20 09:08

    The mapping from (U,V) parameter space to (X,Y,Z) coordinates can be very flexible. Often U is chosen to be something like np.linspace(ll, ul, 100) and is taken to be equal to Y (if y is the axis of rotation). But you don't have to use U that way. Instead U could represent the radius:

    ll, ul = 0, 1 
    u = np.linspace(ll, ul, 100)
    v = np.linspace(0, 2*np.pi, 60)
    U, V = np.meshgrid(u, v)
    

    and then X, Y, Z can be defined in terms of the radius, U:

    Y = U**5 + U**4 + U**3 + U**2 + U
    X = U*np.cos(V)
    Z = U*np.sin(V)
    

    import numpy as np
    import matplotlib.pyplot as plt
    import mpl_toolkits.mplot3d.axes3d as axes3d
    from matplotlib import cm
    
    np.seterr(divide='ignore', invalid='ignore')
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    ll, ul = 0, 1 
    u = np.linspace(ll, ul, 100)
    v = np.linspace(0, 2*np.pi, 60)
    U, V = np.meshgrid(u, v)
    
    Y = U**5 + U**4 + U**3 + U**2 + U
    X = U*np.cos(V)
    Z = U*np.sin(V)
    
    ax.set_xlabel('Y axis')
    ax.set_ylabel('X axis')
    ax.set_zlabel('Z axis')
    
    ax.plot_surface(X, Y, Z, cmap=plt.cm.YlGnBu_r)
    
    plt.show()
    

    0 讨论(0)
提交回复
热议问题