切线与切平面的可视化

切线与切平面的可视化

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)
相关推荐
Suprman17 小时前
从 B/S 到 Electron:平面排版套料系统桌面版开发实战
javascript·平面·electron·排序算法
FPGA信号处理3 天前
【雷达原理与系统】第四节课:雷达波形与脉冲压缩
人工智能·计算机视觉·目标跟踪
蓝宝石的傻话4 天前
MiBeeNVR 的大脑视觉AI:MiBeeVision Lite 发了第一个可用版本
人工智能·计算机视觉·目标跟踪
qq_284274055 天前
间歇运动机构与机械设计概论:棘轮与槽轮机构运动系数、零件失效与强度刚度准则、结构工艺性清根及标准件(含考点)
笔记·学习·平面·自动化·制造
程序员JerrySUN6 天前
Jetson Edge AI 实战05:Secure Boot 到底在保护什么?PKC、SBK、Fuse 与可信启动链一次讲清【视频讲解】
arm开发·人工智能·安全·计算机视觉·目标跟踪·音视频
Earth explosion6 天前
企业级Agent:MCP正在成为新的控制平面
大数据·平面
chaoyuanl7 天前
从平面到包裹:超元力飞行影院如何构建“人在景中、身随景动”的感知闭环
人工智能·科技·平面·xr·vr·娱乐
凌晨1687 天前
YOLO系列学习笔记:从YOLOv1到YOLOv3的演进之路
笔记·yolo·目标跟踪
思茂信息8 天前
CST技术案例:加载正弦曲线的平面螺旋天线仿真研究
算法·平面·emc·cst·电磁仿真·仿真辐射
生活愉甜8 天前
勃肯特2026工博会新品首发——“突破平面,多维精工”
人工智能·平面