代码之家  ›  专栏  ›  技术社区  ›  brienna

如何将二维二次函数绘制为等高线

  •  0
  • brienna  · 技术社区  · 5 年前

    我试图在Matlab中复制下面的曲线图,去掉锯齿线。

    enter image description here

    y = x1^2 + 10 * x2^2 .

    fcontour(@(x1, x2) x1.^2 + 10*x2.^2)
    xlim([-60, 60])
    ylim([-60, 60])
    

    但结果是这样的:

    enter image description here

    在另一次尝试中,我保存等高线图并将其范围设置为如下所示:

    handle = fcontour(@(x1, x2) x1.^2 + 10*x2.^2)
    handle.YRange = [-60, 60]
    handle.XRange = [-60, 60]
    

    这会产生稍微好一点的彩色图,但仍然不对。

    enter image description here

    1 回复  |  直到 5 年前
        1
  •  1
  •   saastn    5 年前

    contour 而不是 fcontour ,可以更好地控制等高线的数量:

    steps = -60:60;
    [x1, x2] = meshgrid(steps, steps);
    fx = x1.^2 + 10*x2.^2;
    contour(x1, x2, fx, 40);
    colormap jet
    

    如果你坚持使用 ,首先需要准备等高线级别列表:

    dim = 60;
    f = @(x1, x2) x1.^2 + 10*x2.^2;
    levels = linspace(0, f(dim, dim), 40);
    fcontour(f, [-dim, dim], 'levellist', levels);
    colormap jet
    

    enter image description here