import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
def f(x):
return x**2
x=np.linspace(-3,3,100)
theta=np.linspace(0,2*np.pi,100)
X,Theta=np.meshgrid(x,theta)
Y=f(X)*np.cos(Theta)
Z=f(X)*np.sin(Theta)
fig=plt.figure(figsize=(6,6))
ax=fig.add_subplot(111,projection='3d')
ax.plot_surface(X,Y,Z,cmap='inferno',edgecolor='none',alpha=0.7)
ax.set_title('3D Surface of Revolution')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
numpy: A numerical library that provides efficient
ways to work with arrays and perform mathematical operations.
def f(x):
return x
** 2
f(x): This defines a function that calculates the
square of the input x, creating a simple parabolic shape when plotted in 2D.
x = np.linspace(-3, 3, 100) # Generate 100 equally spaced values between
-3 and 3
theta = np.linspace(0, 2 * np.pi, 100) # Generate 100 equally spaced values from 0
to 2ฯ (for full revolution)
X, Theta = np.meshgrid(x, theta) # Create a meshgrid from the x and theta
values
x = np.linspace(-3, 3, 100): This generates 100
evenly spaced values between -3 and 3. These represent the x-coordinates for
the paraboloid.
Y = f(X) * np.cos(Theta) # Parametric equation for the Y coordinate
(scaled by cosine of theta)
Z = f(X) * np.sin(Theta) # Parametric equation for the Z coordinate
(scaled by sine of theta)
Y = f(X) * np.cos(Theta): This calculates the
Y-coordinates of the surface. The f(X) part gives the value of the paraboloid
function (square of X), and multiplying by cos(Theta) rotates the paraboloid in
the Y-axis direction.
fig = plt.figure(figsize=(10, 8)) # Create a figure with a size of 10x8 inches
ax = fig.add_subplot(111, projection='3d') # Add a 3D subplot to the figure
fig = plt.figure(figsize=(10, 8)): Initializes a new
figure with a specified size of 10 inches by 8 inches.
ax.plot_surface(X, Y, Z, cmap='inferno',
edgecolor='none', alpha=0.7) # Plot the
3D surface
ax.plot_surface(X, Y, Z, cmap='inferno',
edgecolor='none', alpha=0.7):
ax.set_xlabel("X axis") # Label the X axis
ax.set_ylabel("Y axis") # Label the Y axis
ax.set_zlabel("Z axis") # Label the Z axis
ax.set_title("3D Surface of Revolution
(Paraboloid)") # Set the title of
the plot
ax.set_xlabel("X axis"),
ax.set_ylabel("Y axis"), ax.set_zlabel("Z axis"): Labels
the X, Y, and Z axes, making the plot easier to interpret.
plt.show() #
Show the plot
plt.show(): This line displays the figure. It
renders the 3D surface plot on the screen.


0 Comments:
Post a Comment