切线与切平面的可视化

切线与切平面的可视化

flyfish

切线的可视化

py 复制代码
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation, PillowWriter

# 定义一个简单的一元函数,例如 f(x) = x^2
def func(x):
    return x**2

# 计算函数的导数
def derivative(x):
    return 2 * x

# 设置x的范围
x = np.linspace(-3, 3, 400)
y = func(x)

# 创建图形和轴
fig, ax = plt.subplots()
ax.plot(x, y, 'b', label='f(x) = x^2')
tangent_line, = ax.plot([], [], 'r--', label="Tangent line")  # 切线
point, = ax.plot([], [], 'ro')  # 用于显示切点

# 设置图形的范围
ax.set_xlim(-3, 3)
ax.set_ylim(-1, 10)
ax.legend()

# 初始化函数
def init():
    tangent_line.set_data([], [])
    point.set_data([], [])
    return tangent_line, point

# 更新函数,用于动画
def update(frame):
    x_val = frame / 20.0 - 1.5  # 随时间移动的x值
    
    # 计算切线的点
    y_val = func(x_val)
    slope = derivative(x_val)
    tangent_x = np.linspace(x_val - 1, x_val + 1, 100)
    tangent_y = slope * (tangent_x - x_val) + y_val
    
    # 更新切线和点的位置
    tangent_line.set_data(tangent_x, tangent_y)
    point.set_data([x_val], [y_val])
    
    return tangent_line, point

# 创建动画
ani = FuncAnimation(fig, update, frames=np.arange(0, 120), init_func=init, interval=100, blit=True)

# 保存动画为GIF文件
writer = PillowWriter(fps=10)
ani.save("derivative_animation.gif", writer=writer)

# 显示动画
plt.show()

切平面的可视化

py 复制代码
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import imageio

# 定义二元函数
def f(x, y):
    return x**2 + y**2

# 定义切平面
def tangent_plane(x, y, x0, y0, z0, a, b):
    return z0 + a * (x - x0) + b * (y - y0)

# 计算二元函数的梯度
def gradient(x, y):
    fx = 2 * x
    fy = 2 * y
    return fx, fy

# 设置网格
x = np.linspace(-5, 5, 100)
y = np.linspace(-5, 5, 100)
x, y = np.meshgrid(x, y)
z = f(x, y)

# 选择切平面的点
x0, y0 = 2, 2
z0 = f(x0, y0)
fx, fy = gradient(x0, y0)

# 创建动画
filenames = []
for angle in range(0, 360, 5):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(x, y, z, cmap='viridis', alpha=0.7)
    
    # 画切平面
    z_plane = tangent_plane(x, y, x0, y0, z0, fx, fy)
    ax.plot_surface(x, y, z_plane, color='r', alpha=0.5)
    
    # 标记切点
    ax.scatter(x0, y0, z0, color='r', s=50)
    
    # 设置视角
    ax.view_init(elev=20., azim=angle)
    
    # 保存当前帧
    filename = f'_tmp{angle}.png'
    plt.savefig(filename)
    filenames.append(filename)
    plt.close()

# 创建GIF
with imageio.get_writer('tangent_plane_animation.gif', mode='I', duration=0.1,loop=0) as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)

# 清理临时文件
import os
for filename in filenames:
    os.remove(filename)
相关推荐
图灵学术计算机论文辅导21 小时前
从 “看懂图” 到 “读懂视频”:多模态技术如何用文本反哺视觉?
论文阅读·人工智能·学习·考研·计算机视觉·目标跟踪
终端域名1 天前
元宇宙的三维革命:突破手机平面的数字新境
平面·智能手机·元宇宙
时凯凯凯凯2 天前
使用 Vive Tracker 替代 T265 实现位姿获取(基于 Ubuntu + SteamVR)
linux·ubuntu·目标跟踪·slam
元让_vincent2 天前
论文Review LIO Multi-session Voxel-SLAM | 港大MARS出品!体素+平面特征的激光SLAM!经典必读!
论文阅读·平面·自动驾驶·激光点云·激光slam
2zcode2 天前
基于Matlab的深度学习智能行人检测与统计系统
人工智能·深度学习·目标跟踪
xiaobaibai1533 天前
智慧交通中目标检测 mAP↑28%:陌讯多模态融合算法实战解析
人工智能·算法·目标检测·计算机视觉·目标跟踪·视觉检测
励志成为糕手3 天前
动态置信度调优实战:YOLOv11多目标追踪精度跃迁方案(附完整代码)
人工智能·计算机视觉·目标跟踪
Evand J4 天前
【matlab例程】无迹粒子滤波(UPF)的例程,用于三维环境下多雷达目标跟踪,非线性系统
开发语言·matlab·目标跟踪
沉睡的无敌雄狮4 天前
无人机光伏巡检漏检率↓78%!陌讯多模态融合算法实战解析
人工智能·算法·计算机视觉·目标跟踪
小一亿4 天前
【0基础PS】Photoshop (PS) 理论知识
学习·平面·adobe·信息可视化·photoshop