Skip to main content

A Rose from the Realm of Mathematics

 


Mathematics isn't just about numbers; it's a profound way to explore and represent patterns that resonate with beauty. Recently, I stumbled upon a fascinating way to blend mathematical equations with graphical representation using Python. Let me introduce you to two gorgeous 3D visualizations, both rooted in a simple yet intricate equation.

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Parameters
n = 800
A = 1.995653
B = 1.27689
C = 8
petalNum = 3.6

# Create grid
r = np.linspace(0, 1, n)
theta = np.linspace(-2, 20 * np.pi, n)
R, THETA = np.meshgrid(r, theta)

# Calculate coordinates
x = 1 - (1/2) * ((5/4) * (1 - np.mod(petalNum * THETA, 2 * np.pi) / np.pi)**2 - 1/4)**2
phi = (np.pi/2) * np.exp(-THETA / (C * np.pi))
y = A * (R**2) * (B * R - 1)**2 * np.sin(phi)
R2 = x * (R * np.sin(phi) + y * np.cos(phi))
X = R2 * np.sin(THETA)
Y = R2 * np.cos(THETA)
Z = x * (R * np.cos(phi) - y * np.sin(phi))

# Create colormap
red_map = np.linspace(1, 0.25, 10).reshape(-1, 1)
red_map = np.hstack([red_map, np.zeros_like(red_map), np.zeros_like(red_map)])

# Plot the surface
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap=plt.cm.Reds, linewidth=0, antialiased=False)
ax.view_init(elev=42, azim=-40.5)
plt.show()








import numpy as np
import plotly.graph_objects as go

# Parameters
n = 800
A = 1.995653
B = 1.27689
C = 8
petalNum = 3.6

# Create grid
r = np.linspace(0, 1, n)
theta = np.linspace(-2, 20 * np.pi, n)
R, THETA = np.meshgrid(r, theta)

# Calculate coordinates
x = 1 - (1/2) * ((5/4) * (1 - np.mod(petalNum * THETA, 2 * np.pi) / np.pi)**2 - 1/4)**2
phi = (np.pi/2) * np.exp(-THETA / (C * np.pi))
y = A * (R**2) * (B * R - 1)**2 * np.sin(phi)
R2 = x * (R * np.sin(phi) + y * np.cos(phi))
X = R2 * np.sin(THETA)
Y = R2 * np.cos(THETA)
Z = x * (R * np.cos(phi) - y * np.sin(phi))

# Define the surface
fig = go.Figure(data=[go.Surface(x=X, y=Y, z=Z, colorscale=[[0, 'red'], [1, 'darkred']])])

# Update plot layout for better interactivity
fig.update_layout(scene=dict(
    xaxis_title='X',
    yaxis_title='Y',
    zaxis_title='Z',
))

# Show plot
fig.show()









Comments