matlab中绘制lnx 与1/x 函数图像(deepseek生成)
% 定义 x 的范围 (x > 0)
x = linspace(0.00001, 5, 1000); % 从0.00001开始避免无穷大
% 计算函数值
y_log = log(x); % 自然对数 ln(x)
y_inv = 1 ./ x; % 反比例函数 1/x
% 创建图形窗口
figure('Color', 'white', 'Position', [100, 100, 800, 600]);
% 绘制两个函数(相同坐标轴)
plot(x, y_log, 'b-', 'LineWidth', 2, 'DisplayName', 'y = ln(x)');
hold on;
plot(x, y_inv, 'r--', 'LineWidth', 2, 'DisplayName', 'y = 1/x');
% 设置坐标轴范围
xlim([0, 5]); % 横轴范围 0-5
ylim([-10, 10]); % 纵轴范围 -10 到 10(按您的要求)
% 添加坐标轴和原点
ax = gca;
ax.XAxisLocation = 'origin'; % X轴穿过原点
ax.YAxisLocation = 'origin'; % Y轴穿过原点
box off; % 移除边框
% 标记特殊点
plot(1, 0, 'ko', 'MarkerSize', 8, 'MarkerFaceColor', 'k', 'DisplayName', '(1,0)'); % ln(1)=0
plot(1, 1, 'ko', 'MarkerSize', 8, 'MarkerFaceColor', 'k', 'HandleVisibility', 'off'); % 1/x在(1,1)
% 计算并标记交点(ln(x) = 1/x 的解)
x_intersect = fzero(@(x) log(x) - 1/x, 1.5); % 数值求解交点
y_intersect = log(x_intersect);
plot(x_intersect, y_intersect, 'go', 'MarkerSize', 8, 'MarkerFaceColor', 'g', ...
'DisplayName', sprintf('交点 (%.3f, %.3f)', x_intersect, y_intersect));
% 添加渐近线标注
text(0.05, -9.5, 'x→0⁺: ln(x)→-∞', 'FontSize', 10, 'Color', 'b');
text(0.05, 9.5, 'x→0⁺: 1/x→∞', 'FontSize', 10, 'Color', 'r');
text(4.8, 0.2, 'y=0 渐近线', 'FontSize', 10, 'Color', [0.5 0.5 0.5]);
% 添加参考线
line([0 5], [0 0], 'Color', [0.5 0.5 0.5], 'LineStyle', ':', 'HandleVisibility', 'off'); % x轴
line([1 1], [-10 10], 'Color', [0.5 0.5 0.5], 'LineStyle', ':', 'HandleVisibility', 'off'); % x=1竖线
% 添加标注
xlabel('x', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('y', 'FontSize', 12, 'FontWeight', 'bold');
title('ln(x) 与 1/x 函数比较 (相同坐标轴)', 'FontSize', 14, 'FontWeight', 'bold');
legend('show', 'Location', 'northeast');
grid on;