「用Python来学微积分」2. 函数图像的变换

已知一个函数的图像,通过适当的变换,得到另一个与之相关的函数的图像,这样的绘图方法叫做图像变换,常用的图像变换是平移变换、对称变换 和 伸缩变换。

一、 平移变换

1. 左平移

将函数 y = f(x) 向左平移 m( m > 0) 个单位长度, 得到函数 y = f(x + m) 的图像,如下

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-4, 10), figsize=(8, 6))

# 添加一个二次函数的图像
x = np.linspace(0.5, 5.5, 100)
y = (x-3)**2 - 2

# 向左平移6个单位,(x - 3) + 6 = x + 3
x1 = np.linspace(-5.5, -0.5, 100)
y1 = (x1+3)**2 - 2

# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = f(x+m)', color='red')
ax.legend()
plt.show()

2. 右平移

将函数 y = f(x) 向右平移 m( m > 0) 个单位长度, 得到函数 y = f(x - m) 的图像,如下

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-4, 10), figsize=(8, 6))

# 添加一个二次函数的图像
x = np.linspace(-5.5, -0.5, 100)
y = (x+3)**2 - 2

# 向右平移6个单位,(x + 3) - 6 = x - 3
x1 = np.linspace(0.5, 5.5, 100)
y1 = (x1-3)**2 - 2

# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = f(x-m)', color='red')
ax.legend()
plt.show()

运行结果:

3. 上平移

将函数 y = f(x) 向上平移 n( n > 0) 个单位长度, 得到函数 y = f(x) + n 的图像,如下

Python 代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-4, 10), figsize=(8, 6))

# 添加一个二次函数的图像
x = np.linspace(-3, 3, 100)
y = x**2 - 2

# 向上移动4个单位,-2 + 4 = 2
x1 = np.linspace(-3, 3, 100)
y1 = x1**2 + 2

# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = f(x) + n', color='red')

ax.legend()
plt.show()

运行结果:

4. 下平移

将函数 y = f(x) 向左平移 n( n > 0) 个单位长度, 得到函数 y = f(x) - n 的图像,如下

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-4, 10), figsize=(8, 6))
# 添加一个二次函数的图像
x = np.linspace(-3, 3, 100)
y = x**2 + 2

# 向下移动4个单位,2 - 4 = -2
x1 = np.linspace(-3, 3, 100)
y1 = x1**2 - 2

# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = f(x) - n', color='red')
ax.legend()
plt.show()

二、 对称变换

1. 关于x轴对称

将函数 y = f(x) 的图像关于 x 轴对称,得到函数 y = -f(x) 的图像

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-5, 5), y_range=(-8, 8), figsize=(6, 8))

# 添加一个二次函数的图像
x = np.linspace(-1, 3, 100)
y = (x-1)**2 + 1
# 关于X轴对称,-((x-1)**2 + 1) = -(x-1)**2 - 1
x1 = np.linspace(-1, 3, 100)
y1 = -(x1-1)**2 - 1

# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = -f(x)', color='red')

ax.legend()
plt.show()

运行结果:

2. 关于y轴对称

将函数 y = f(x) 的图像关于 y 轴对称,得到函数 y = f(-x) 的图像

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-10, 10), figsize=(8, 8))

# 添加一个二次函数的图像
x = np.linspace(-1, 5, 100)
y = (x-2)**2 - 2

# 关于Y轴对称,(-x-2)**2 = (x+2)**2
x1 = np.linspace(-5, 1, 100)
y1 = (x1+2)**2 - 2

# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = f(-x)', color='red')
ax.legend()
plt.show()

运行结果:

3. 关于原点对称

将函数 y = f(x) 的图像关于原点对称,得到函数 y = -f(-x) 的图像

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-10, 10), figsize=(8, 8))

# 添加一个二次函数的图像
x = np.linspace(-1, 5, 100)
y = (x-2)**2 - 2

# 关于Y轴对称,-((-x-2)**2 -2) = 2-(x+2)**2
x1 = np.linspace(-5, 1, 100)
y1 = 2-(x1+2)**2
# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = -f(-x)', color='red')
ax.legend()
plt.show()

运行结果:

4. 关于执行 y = x 对称

将函数 y = f(x) 的图像关于直线 y = x 对称,得到函数 y=f−1(x)y = f^-1(x)y=f−1(x) 的图像

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 支持中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-10, 10), figsize=(8, 8))

# 添加一个二次函数的图像
x = np.linspace(-1, 5, 100)
y = (x-2)**2 - 2
# x 和 y 互换
x1 = y
y1 = x
# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black')
ax.plot(x1, y1, 'r-', linewidth=2, label='关于y=x对称的函数', color='red')
ax.legend()
plt.show()

运行结果:

5. 将x轴下方对称到x轴上方

保留函数 y = f(x) 在 x 轴及 x 轴上方的部分, 把 x 轴下方的部分关于 x 轴对称到 x 轴上方(去掉原来下方的部分), 得到函数 y = |f(x)| 的图像

Python 代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-10, 10), figsize=(8, 8))
# 添加一个二次函数的图像
x = np.linspace(-1, 5, 100)
y = (x-2)**2 - 2

# y = |f(x)|
x1 = np.linspace(-1, 5, 100)
y1 = abs((x1-2)**2 - 2)
# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black', linestyle='--')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = -f(-x)', color='red')
ax.legend()
plt.show()

运行结果:

6. y轴右边对称到y轴左边

保留函数 y = f(x) 在y轴及y轴右侧的部分,去掉 y 轴左侧的部分,再将 y 轴右侧图像对称到 y 轴左侧,得到函数 y = f(|x|) 的图像

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-10, 10), figsize=(8, 8))
# 添加一个二次函数的图像
x = np.linspace(-5, 5, 100)
y = (x-2)**2 - 2
# y = f(|x|)
x1 = np.linspace(-5, 5, 100)
y1 = (abs(x1)-2)**2 - 2

# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black', linestyle='--')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = f(|x|)', color='red')
ax.legend()
plt.show()

运行结果:

三、伸缩变换

1. 水平伸缩

y = f(wx)(w > 1)的图像, 可由 y = f(x) 的图像上每点的横坐标缩短到原来的 1/w 倍(纵坐标不变)得到;

y = f(wx)(0 < w < 1)的图像,可由 y = f(x) 的图像上每点的横坐标伸长到原来的 1/w 倍(纵坐标不变)得到;

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system

# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-10, 10), figsize=(8, 8))

# 添加一个二次函数的图像
x = np.linspace(-1, 5, 100)
y = (x-2)**2 - 2

# y = f(2x) 横坐标缩小为1/2倍
x1 = np.linspace(-1, 5, 100)
y1 = (2*x1-2)**2 - 2

# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black', linestyle='--')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = f(2x)', color='red')
ax.legend()
plt.show()

运行结果:

2. 垂直伸缩

y = Af(wx)(A > 1)的图像, 可由 y = f(x) 的图像上每点的纵坐标伸长到原来的 A 倍(横坐标不变)得到;

y = Af(wx)(0 < A < 1)的图像, 可由 y = f(x) 的图像上每点的纵坐标缩短到原来的 A 倍(横坐标不变)得到;

Python代码:

python 复制代码
import matplotlib.pyplot as plt
import numpy as np
from test0 import create_coordinate_system
# 创建一个坐标系
fig, ax = create_coordinate_system(x_range=(-10, 10), y_range=(-10, 10), figsize=(8, 8))
# 添加一个二次函数的图像
x = np.linspace(-1, 5, 100)
y = (x-2)**2 - 2
# y = f(x)/2 纵坐标缩小为1/2倍
x1 = np.linspace(-1, 5, 100)
y1 = (1/2)*((x-2)**2 - 2)
# 绘制二次函数的图像
ax.plot(x, y, 'r-', linewidth=2, label='y = f(x)', color='black', linestyle='--')
ax.plot(x1, y1, 'r-', linewidth=2, label='y = f(x)/2', color='red')
ax.legend()
plt.show()

执行结果:

三、总结

后续我将继续使用Python实现扈志明《微积分》教材中的更多内容,包括极限、导数、积分等核心概念,通过编程实践深化对微积分知识的理解。

参考教材:扈志明,《微积分》,高等教育出版社。本文中的数学定义和概念均来源于此教材。

相关推荐
知行合一。。。27 分钟前
Python--04--数据容器(总结)
开发语言·python
架构师老Y31 分钟前
008、容器化部署:Docker与Python应用打包
python·容器·架构
lifewange1 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
pluvium271 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
2401_827499991 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉2 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构
ZhengEnCi2 小时前
P2G-Python字符串方法完全指南-split、join、strip、replace的Python编程利器
python
是小蟹呀^2 小时前
【总结】LangChain中工具的使用
python·langchain·agent·tool
宝贝儿好2 小时前
【LLM】第二章:文本表示:词袋模型、小案例:基于文本的推荐系统(酒店推荐)
人工智能·python·深度学习·神经网络·自然语言处理·机器人·语音识别
王夏奇2 小时前
pythonUI界面弹窗设置的几种办法
python·ui