Saturday, 3 May 2025

3D Butterfly Wing (Mathematical Model) using Python

 


import matplotlib.pyplot as plt

import numpy as np

u=np.linspace(0,2*np.pi,100)

v=np.linspace(-np.pi/2,np.pi/2,100)

u,v=np.meshgrid(u,v)

x=np.sin(u)*(1+0.5*np.cos(v))*np.cos(v)

y=np.cos(u)*(1+0.5*np.cos(v))*np.cos(v)

z=np.sin(v)+0.2*np.sin(3*u)

fig=plt.figure(figsize=(6,6))

ax=fig.add_subplot(111,projection='3d')

ax.plot_surface(x,y,z,cmap='coolwarm',edgecolor='k',alpha=0.9)

ax.set_title('3D Butterfly Wing')

ax.set_xlabel('X axis')

ax.set_ylabel('Y axis')

ax.set_zlabel('Z axis')

ax.set_box_aspect([1,1,0.5])

plt.tight_layout()

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Importing Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy: Used for creating numerical arrays and trigonometric functions.

 matplotlib.pyplot: Used for creating the 3D plot.

 2. Creating Parameter Grids

u = np.linspace(0, 2 * np.pi, 100)

v = np.linspace(-np.pi / 2, np.pi / 2, 100)

u, v = np.meshgrid(u, v)

u: Controls the angular direction (think of it like horizontal spread of the wing).

 v: Controls the vertical sweep or curvature of the wings.

 meshgrid: Creates a 2D grid to evaluate the parametric surface over.

 3. Defining the Parametric Equations

x = np.sin(u) * (1 + 0.5 * np.cos(v)) * np.cos(v)

y = np.cos(u) * (1 + 0.5 * np.cos(v)) * np.cos(v)

z = np.sin(v) + 0.2 * np.sin(3 * u)

These equations build a curved, sinusoidal surface that resembles butterfly wings:

 x and y: Give a spiraling shape using a modified polar coordinate system.

 The term (1 + 0.5 * cos(v)) * cos(v) gives depth and curvature.

 z: Controls the vertical deformation, making the wings appear "flapping" or "wavy."

 sin(v) gives the main vertical structure.

 0.2 * sin(3 * u) adds a ripple or flutter pattern, mimicking wing detail.

 4. Setting Up the 3D Plot

fig = plt.figure(figsize=(6, 6))

ax = fig.add_subplot(111, projection='3d')

Creates a square figure with a 3D plotting environment.

 5. Plotting the Surface

ax.plot_surface(x, y, z, cmap='coolwarm', edgecolor='k', alpha=0.9)

plot_surface: Draws the 3D shape.

 cmap='coolwarm': Uses a smooth gradient of blue to red.

 edgecolor='k': Adds a black gridline for better surface structure visibility.

 alpha=0.9: Slight transparency for softness.

 6. Customizing the Axes

ax.set_title('3D Butterfly Wings (Mathematical Model)', fontsize=14)

ax.set_xlabel('X axis')

ax.set_ylabel('Y axis')

ax.set_zlabel('Z axis')

ax.set_box_aspect([1, 1, 0.5])

Adds title and axis labels.

 set_box_aspect([1, 1, 0.5]): Makes the Z-axis compressed to enhance the wing appearance.

7. Show the Plot

plt.tight_layout()

plt.show()

tight_layout(): Adjusts padding between plot elements.

 show(): Displays the 3D plot.

 

 


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)