1. 天空中的彩虹
当太阳光穿过雨滴时,会发生折射、反射和再次折射,这三种过程共同作用,使得白光分解成七种颜色的光谱,形成我们常见的彩虹。这个过程可以用以下简单的代码来模拟:
import matplotlib.pyplot as plt
import numpy as np
def simulate_rainbow(angle_of_incidence):
# 定义折射率
refractive_index = 1.33
# 定义白光波长范围
wavelengths = np.linspace(400, 700, 100)
# 计算折射角
angles_of_refraction = np.arcsin(np.sin(angle_of_incidence) / refractive_index)
# 计算颜色
colors = [plt.cm.viridis(wavelength / 700) for wavelength in wavelengths]
return colors
# 模拟太阳光以45度角入射
colors = simulate_rainbow(np.radians(45))
plt.imshow(np.array(colors).T, aspect='auto')
plt.axis('off')
plt.show()
2. 水中的倒影
当光线从空气进入水中时,会发生折射,导致我们看到的物体位置发生偏移。这种现象可以用以下简单的代码来模拟:
import matplotlib.pyplot as plt
import numpy as np
def simulate_mirror(x, y, focal_length):
# 计算折射角
angle_of_incidence = np.arctan(y / x)
angle_of_refraction = np.arcsin(np.sin(angle_of_incidence) / 1.33)
# 计算倒影位置
mirrored_x = focal_length * np.tan(angle_of_refraction)
mirrored_y = -focal_length
return mirrored_x, mirrored_y
# 模拟一个物体在水面下的位置
x, y = 1, 1
focal_length = 1
mirrored_x, mirrored_y = simulate_mirror(x, y, focal_length)
plt.plot([x, mirrored_x], [y, mirrored_y], 'r')
plt.plot([0, 2], [0, 0], 'k--')
plt.plot([0, 0], [0, 2], 'k--')
plt.axis('equal')
plt.show()
3. 烟花绽放的原理
烟花绽放的原理是利用化学反应产生高温高压气体,从而产生各种颜色的火花。以下是一个简单的烟花模拟代码:
import matplotlib.pyplot as plt
import numpy as np
def simulate_firework(x, y, colors):
# 定义烟花爆炸的半径
radius = 0.1
# 计算烟花爆炸的火花位置
spark_positions = np.array([[x + np.random.uniform(-radius, radius), y + np.random.uniform(-radius, radius)] for _ in range(100)])
# 计算火花颜色
spark_colors = np.array([colors[np.random.randint(0, len(colors))] for _ in range(100)])
return spark_positions, spark_colors
# 模拟一个烟花在空中的位置
x, y = 0, 0
colors = ['red', 'green', 'blue', 'yellow', 'purple']
spark_positions, spark_colors = simulate_firework(x, y, colors)
plt.scatter(spark_positions[:, 0], spark_positions[:, 1], c=spark_colors)
plt.axis('equal')
plt.show()
通过以上几个例子,我们可以看到,日常生活中的神奇自然现象其实都蕴含着丰富的科学原理。只要我们用心去观察,用科学的方法去解释,就能轻松掌握这些现象背后的奥秘。
