在MATLAB上绘制隐式函数x + y - log(x) - log(y)-2 = 0

时间:2017-02-06 20:39:13

标签: matlab plot graph

我想在Matlab上绘制上述函数,所以我使用了以下代码

ezplot('-log(x)-log(y)+x+y-2',[-10 10 -10 10]);

但是我只是得到一个空白的屏幕。但显然至少有一点(1,1)满足等式。 我不认为绘图仪设置有问题,因为我正在获取像

这样的函数的图形
ezplot('-log(y)+x+y-2',[-10 10 -10 10]); 

我没有足够的代表来嵌入图片:)

2 个答案:

答案 0 :(得分:6)

如果我们在您的函数上使用solve,我们可以看到两个点,其函数等于零。这些点位于(1, 1)(0.3203 + 1.3354i, pi)

syms x y
result = solve(-log(x)-log(y)+x+y-2, x, y);

result.x
% -wrightOmega(log(1/pi) - 2 + pi*(1 - 1i))
%                                         1

result.y
%   pi
%    1

如果仔细观察你的功能,我们可以看到这些值实际上是复杂的

[x,y] = meshgrid(-10:0.01:10, -10:0.01:10);
values = -log(x)-log(y)+x+y-2;

whos values
%  Name           Size                 Bytes  Class     Attributes
%  values      2001x2001            64064016  double    complex

似乎在早期版本的MATLAB中,ezplot仅通过考虑数据的 real 组件来处理复杂函数。因此,这将产生以下情节

enter image description here

但是,较新的版本会考虑数据的幅度,只有当实部和虚部都为零时才会出现零。在这两点中,只有一点是真实的并且能够绘制;但是,ezplot的相对粗略的采样无法显示该单个点。

您可以使用contourc来确定此点的位置

imagesc(abs(values), 'XData', [-10 10], 'YData', [-10 10]);
axis equal
hold on

cmat = contourc(abs(values), [0 0]);
xvalues = xx(1, cmat(1,2:end));
yvalues = yy(cmat(2,2:end), 1);

plot(xvalues, yvalues, 'r*')

enter image description here

答案 1 :(得分:3)

这是因为x = y = 1是给定方程式的唯一解决方案。

请注意minimum value of x - log(x) is 1 and that happens when x = 1。显然,y - log(y)也是如此。因此,-log(x)-log(y)+x+y总是大于2,但x = y = 1除外,它恰好等于2.

由于你的方程只有一个解,所以图上没有线。

为了使这个可视化,让我们绘制方程式

ezplot('-log(x)-log(y)+x+y-C',[-10 10 -10 10]);

表示C的各种值。

% choose a set of values between 5 and 2
C = logspace(log10(5), log10(2), 20);

% plot the equation with various values of C
figure
for ic=1:length(C)
    ezplot(sprintf('-log(x)-log(y)+x+y-%f', C(ic)),[0 10 0 10]);
    hold on
end
title('-log(x)-log(y)+x+y-C = 0, for 5 < C < 2');

enter image description here

请注意,C = 5获得了最大的曲线。随着C的值减小,曲线也会变小,直到C = 2它完全消失。