Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
564 views
in Technique[技术] by (71.8m points)

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

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 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, 60)
v = np.linspace(0, 2*np.pi, 60)
U, V = np.meshgrid(u, v)

Z = U

X = np.sqrt(Z)*np.cos(V)
Y = np.sqrt(Z)*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()

This code just plots the solid of revolution of the function x = sqrt(y) which is the inverse of y = x**2. But how to draw the solid of revolution for a function as:

y = x**5 + x**4 + x**3 + x**2 + x

?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...