绘制函数 y=f(x)=x^3−1/x 和其在 x=1 处切线的图像

绘制函数 y=f(x)=x^3−1/x 和其在 x=1 处切线的图像。

%matplotlib inline
import numpy as np
from IPython import display
from d2l import torch as d2l


def f(x):
    return x ** 3 - 1 / x

use_svg_display函数指定matplotlib软件包输出svg图表以获得更清晰的图像

def use_svg_display():  #@save
    """使用svg格式在Jupyter中显示绘图。"""
    display.set_matplotlib_formats('svg')

定义set_figsize函数来设置图表大小

def set_figsize(figsize=(3.5, 2.5)):  #@save

  """设置matplotlib的图表大小。"""

  use_svg_display()

  d2l.plt.rcParams['figure.figsize'] = figsize

set_axes函数用于设置由matplotlib生成图表的轴的属性

#@save
def set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend):
    """设置matplotlib的轴。"""
    axes.set_xlabel(xlabel)
    axes.set_ylabel(ylabel)
    axes.set_xscale(xscale)
    axes.set_yscale(yscale)
    axes.set_xlim(xlim)
    axes.set_ylim(ylim)
    if legend:
        axes.legend(legend)
    axes.grid()
#@save
def plot(X, Y=None, xlabel=None, ylabel=None, legend=None, xlim=None,
         ylim=None, xscale='linear', yscale='linear',
         fmts=('-', 'm--', 'g-.', 'r:'), figsize=(3.5, 2.5), axes=None):
    """绘制数据点。"""
    if legend is None:
        legend = []

    set_figsize(figsize)
    axes = axes if axes else d2l.plt.gca()

    # 如果 `X` 有一个轴,输出True
    def has_one_axis(X):
        return (hasattr(X, "ndim") and X.ndim == 1 or isinstance(X, list)
                and not hasattr(X[0], "__len__"))

    if has_one_axis(X):
        X = [X]
    if Y is None:
        X, Y = [[]] * len(X), X
    elif has_one_axis(Y):
        Y = [Y]
    if len(X) != len(Y):
        X = X * len(Y)
    axes.cla()
    for x, y, fmt in zip(X, Y, fmts):
        if len(x):
            axes.plot(x, y, fmt)
        else:
            axes.plot(y, fmt)
    set_axes(axes, xlabel, ylabel, xlim, ylim, xscale, yscale, legend)
x = np.arange(0, 3, 0.1)
plot(x, [f(x), 4 * x - 4], 'x', 'f(x)', legend=['f(x)', 'Tangent line (x=1)'])

在这里插入图片描述

在Matlab中,你可以通过求导点斜式来绘制函数y=ln(x)+1在x=1切线。首先,我们需要找到切点的坐标切线的斜率。 1. 导数计算: 对于函数`y = ln(x) + 1`,其导数`dy/dx`就是对`ln(x)`求导得到的结果加上常数1的导数,即`1/x`。 2. 切点坐标: 当x=1时,代入原函数求出对应的y值。所以切点为`(1, ln(1) + 1)`,因为`ln(1)`等于0,所以切点就是`(1, 1)`。 3. 斜率计算: 切线斜率就是该点的导数值,在x=1斜率为1。 4. 绘制切线: 有了切点斜率,可以使用点斜式`y - y1 = m * (x - x1)`来表示切线方程,其中`(x1, y1)`是切点,m是斜率。然后,可以画出这条直线。 下面是一个简单的步骤实现: ```matlab % 定义原函数 y = @(x) log(x) + 1; % 求切点 x_c = 1; y_c = y(x_c); % 求斜率 dy_dx = 1./x_c; % 创建点斜式方程 line_x = [x_c - 1; x_c + 1]; line_y = dy_dx * (line_x - x_c) + y_c; % 绘制函数切线 plot(x, y(x), 'b', line_x, line_y, 'r'); % blueline是原函数,redline是切线 hold on; grid on; % 添加切线标记 text(x_c, y_c, ['切点 (', num2str(x_c), ', ', num2str(y_c), ')'], 'HorizontalAlignment', 'center'); % 设置坐标轴范围标题 xlim([0.9 1.1]); ylim([-0.1 2.1]); xlabel('x'); ylabel('y'); title('y = ln(x) + 1在x=1切线'); % 关闭原函数图像 hold off; ``` 运行以上代码后,你会看到原函数y=ln(x)+1及其在x=1切线
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

如果我是泡橘子

很高兴我踩过的坑能够帮助到你!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值