You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MatLab绘制函数序列异常:无法将fn限制在x±0.1范围内

函数序列绘图异常问题

我要绘制一组函数序列,该序列理论上应该被直线x+0.1和x-0.1包裹,但实际绘图结果并未达到这个预期。以下是我的MatLab代码:

n_max = 10;
x = linspace(0, 1, 1000);
fn_values = zeros(n_max, length(x));

for n = 1:n_max
    fn = zeros(size(x));
    for k = 0:(2^n - 1)
        Ik = [(k / 2^n), ((k + 1) / 2^n)];
        fn = fn + (k / 2^n) * (x >= Ik(1) & x < Ik(2));
    end
    fn_values(n, :) = fn;
end

figure;
hold on; 
for n = 1:n_max
    plot(x, fn_values(n, :), 'DisplayName', ['f_', num2str(n)]);
end

plot(x, x + 0.1, 'r', 'DisplayName', 'x + 0.1');
plot(x, x - 0.1, 'g', 'DisplayName', 'x - 0.1');

title('graph of f_n(x) and x + 0.1, x - 0.1');
xlabel('x');
ylabel('f_n(x)');
legend('show');
grid on;
hold off;

实际生成的图像如下:

函数序列绘图效果

请问我的代码哪里出问题了?希望能得到帮助!


内容的提问来源于stack exchange,提问作者Wrlord

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 03:42:34