在上一章中,我们将函数看作是将输入映射到输出的方法,并研究了极限以明白函数在特定点附近的行为。现在,我们将把重心放在函数输出如何随输入的变化而变化上。想想开车的情形:你的位置随时间变化。有时你会加速,有时你会减速。微积分提供了精确描述这些变化的工具。我们通常用率来描述变化。例如,速度是距离相对于时间的变化率。但衡量这种率有不同的方法。平均变化率:整体情况想象你在一次公路旅行中。你下午1:00从里程标10英里处出发,下午3:00到达里程标130英里处。你的平均速度是多少?你行驶的距离是 $130 - 10 = 120$ 英里。 所用时间是下午 $3:00 - $ 下午 $1:00 = 2$ 小时。你的平均速度是:$$ \text{平均速度} = \frac{\text{总距离}}{\text{总时间}} = \frac{120 \text{ 英里}}{2 \text{ 小时}} = 60 \text{ 英里每小时} $$这是你在这2小时内位置的平均变化率。它让你大致了解旅行中位置变化的快慢,但它并没有告诉你是否一直以精确的60英里每小时的速度行驶。你可能停下来加油,或者在开阔的高速公路上加速了。从数学上看,如果我们有一个函数 $f(x)$,在两个点(比如 $x_1$ 和 $x_2$)之间的平均变化率,是函数输出($y$ 值)的变化量除以输入($x$ 值)的变化量。我们常用希腊字母德尔塔 ($\Delta$) 来表示“变化量”。因此,$\Delta y$ 表示 $y$ 的变化量,$\Delta x$ 表示 $x$ 的变化量。公式是:$$ \text{平均变化率} = \frac{\Delta y}{\Delta x} = \frac{f(x_2) - f(x_1)}{x_2 - x_1} $$从几何上看,这个公式计算的是函数图像上连接 $(x_1, f(x_1))$ 和 $(x_2, f(x_2))$ 这两点的直线的斜率。这条线被称为割线。我们来看一个简单函数,$f(x) = x^2$。在 $x_1 = 1$ 和 $x_2 = 3$ 之间的平均变化率是多少?首先,找出对应的 $y$ 值: $f(1) = 1^2 = 1$ $f(3) = 3^2 = 9$现在,应用公式: $$ \text{平均变化率} = \frac{f(3) - f(1)}{3 - 1} = \frac{9 - 1}{2} = \frac{8}{2} = 4 $$ 因此,平均而言,在 $x=1$ 到 $x=3$ 之间,函数 $f(x) = x^2$ 的 $y$ 值每当 $x$ 增加1个单位,就增加4个单位。{"layout": {"title": "f(x) = x^2 的平均变化率", "xaxis": {"title": "x", "range": [-0.5, 3.5]}, "yaxis": {"title": "f(x)", "range": [-1, 10]}, "showlegend": true}, "data": [{"x": [1, 3], "y": [1, 9], "mode": "lines+markers", "type": "scatter", "name": "割线 (平均变化率)", "line": {"color": "#f03e3e", "width": 2}, "marker": {"color": "#f03e3e", "size": 8}}, {"x": [-0.5, 0, 1, 2, 3, 3.5], "y": [0.25, 0, 1, 4, 9, 12.25], "mode": "lines", "type": "scatter", "name": "f(x) = x^2", "line": {"color": "#1c7ed6"}}]}红线连接了函数 $f(x) = x^2$ 图像上的点 (1, 1) 和 (3, 9)。它的斜率表示 $x=1$ 和 $x=3$ 之间的平均变化率。瞬时变化率:瞬时视角平均变化率很有用,但通常我们需要知道某物在特定瞬间此刻变化的速度。如果你看汽车的速度计,它显示的是你精确的瞬时速度,而不是你过去一小时的平均速度。这就是瞬时变化率。我们如何求出单一点(例如 $x = x_1$)的变化率?我们的平均变化率公式 $\frac{f(x_2) - f(x_1)}{x_2 - x_1}$ 遇到了问题。如果我们尝试通过让 $x_2$ 靠近 $x_1$ 来使间隔越来越小,最终 $x_2$ 将等于 $x_1$。这会使分母 $x_2 - x_1$ 变为零,而我们不能除以零。这就是第一章中极限派上用场的地方。我们不将 $x_2$ 设为等于 $x_1$,而是问:当 $x_2$ 任意接近 $x_1$ 时,平均变化率会趋向什么值?想想图像上的割线。当我们把点 $(x_2, f(x_2))$ 越来越近地移向 $(x_1, f(x_1))$ 时,割线会旋转。它在极限情况下趋近的线被称为点 $(x_1, f(x_1))$ 处的切线。这条切线在该单一点接触曲线,并表示曲线在该精确位置的方向。这条切线的斜率就是函数在 $x = x_1$ 处的瞬时变化率。{"layout": {"title": "割线逼近切线", "xaxis": {"title": "x", "range": [-0.5, 3.5]}, "yaxis": {"title": "f(x)", "range": [-1, 10]}, "showlegend": false, "updatemenus": [{"buttons": [{"args": [null, {"frame": {"duration": 500, "redraw": true}, "fromcurrent": true, "transition": {"duration": 300, "easing": "quadratic-in-out"}}], "label": "播放", "method": "animate"}, {"args": [[null], {"frame": {"duration": 0, "redraw": true}, "mode": "immediate", "transition": {"duration": 0}}], "label": "暂停", "method": "animate"}], "direction": "left", "pad": {"r": 10, "t": 87}, "showactive": false, "type": "buttons", "x": 0.1, "xanchor": "right", "y": 0, "yanchor": "top"}]}, "data": [{"x": [-0.5, 0, 1, 2, 3, 3.5], "y": [0.25, 0, 1, 4, 9, 12.25], "mode": "lines", "type": "scatter", "line": {"color": "#1c7ed6"}}, {"x": [1, 1], "y": [1, 1], "mode": "markers", "type": "scatter", "marker": {"color": "#fa5252", "size": 8}}, {"x": [1, 3], "y": [1, 9], "mode": "lines", "type": "scatter", "line": {"color": "#fd7e14", "width": 2, "dash": "dot"}}, {"x": [1, 2.5], "y": [1, 6.25], "mode": "lines", "type": "scatter", "line": {"color": "#fab005", "width": 2, "dash": "dot"}}, {"x": [1, 2], "y": [1, 4], "mode": "lines", "type": "scatter", "line": {"color": "#74b816", "width": 2, "dash": "dot"}}, {"x": [1, 1.5], "y": [1, 2.25], "mode": "lines", "type": "scatter", "line": {"color": "#1098ad", "width": 2, "dash": "dot"}}, {"x": [0.5, 1.5], "y": [-1, 3], "mode": "lines", "type": "scatter", "line": {"color": "#be4bdb", "width": 3}}], "frames": [{"name": "x2=3", "data": [{"x": [1, 3], "y": [1, 9], "mode": "lines+markers", "marker": {"color": "#fd7e14", "size": [0, 8]}, "line": {"color": "#fd7e14", "width": 2, "dash": "solid"}}]}, {"name": "x2=2.5", "data": [{"x": [1, 2.5], "y": [1, 6.25], "mode": "lines+markers", "marker": {"color": "#fab005", "size": [0, 8]}, "line": {"color": "#fab005", "width": 2, "dash": "solid"}}]}, {"name": "x2=2", "data": [{"x": [1, 2], "y": [1, 4], "mode": "lines+markers", "marker": {"color": "#74b816", "size": [0, 8]}, "line": {"color": "#74b816", "width": 2, "dash": "solid"}}]}, {"name": "x2=1.5", "data": [{"x": [1, 1.5], "y": [1, 2.25], "mode": "lines+markers", "marker": {"color": "#1098ad", "size": [0, 8]}, "line": {"color": "#1098ad", "width": 2, "dash": "solid"}}]}, {"name": "Tangent", "data": [{"x": [0.5, 1.5], "y": [-1, 3], "mode": "lines", "line": {"color": "#be4bdb", "width": 3}}]}]}当第二个点(橙色、黄色、绿色、青色标记)越来越靠近第一个点(x=1 处的红色标记)时,割线的斜率趋近于紫色切线的斜率。这条切线的斜率表示在 x=1 处的瞬时变化率。(点击“播放”观看动画。)这种瞬时变化率正是我们所说的函数在某一点的导数。它告诉我们,函数输出对于其输入在该位置的微小变化有多敏感。明白这种区别意义重大。在机器学习中,我们通常想知道模型参数(如神经网络中的权重)的微小变化将如何影响模型此刻的误差。我们通常不关心大幅调整带来的平均变化;我们希望了解瞬时变化,以指导我们如何进行下一次微小调整来改进模型。这需要掌握瞬时变化率,它直接引向后续章节中导数的定义和计算。