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

How to plot 2D quadratic function as a contour

我正在尝试在 Matlab 中复制以下图,减去之字形线。

我知道这是一个two-dimensional二次函数y = x1^2 + 10 * x2^2

我最好的尝试是

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

但这会产生这样的图表:

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

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

这使得绘图的颜色略好一些,但仍然不正确。

如何修正我的代码以到达第一个图?

使用contour代替fcontour,这样可以更好地控制轮廓线的数量:

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

但是如果你坚持使用fcontour,你需要先准备好等高线等级列表:

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