如何将2D二次函数绘制为轮廓

问题描述

我正在尝试在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

如何修复代码以得出第一个图?

解决方法

使用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

enter image description here