如何在 Julia 中使用 Gadfly 以一般形式绘制二维函数?

How to plot a 2D function in general form using Gadfly in Julia?

我学会了如何在 "deterministic form" 中绘制函数(比如 y = a0x + a1x^2 + a2x^3 ...)。我想知道如何以一般形式绘制函数(如 ax + by + c = 0)?

对于一般形式的一些简单函数,我们可以将其转换为 "deterministic form",然后将数学函数转换为 Julia 函数(如 ax + by + c = 0 到 y = (ax + c)/(-b))。但是,对于一些复杂的函数,编写一个Julia函数并不容易。

例如:

有什么方法可以使用 Gadfly 绘制此函数?

Mattriks suggests to use Geom.contour to plot functions in a general form in this issue.

我已经在示例中尝试过了,效果很好。

例如,在问题的函数中:

假设参数列表是

para=[1.27274,0.625272,1.18109,-2.01996,-0.917423,-1.43166,0.124006,-0.365534,-0.357239,-0.175131,-1.45816,-0.0509896,-0.615555,-0.274707,-1.19282,-0.242188,-0.206006,-0.0447305,-0.277784,-0.295378,-0.456357,-1.0432,0.0277715,-0.292431,0.0155668,-0.327379,-0.143887,-0.924652]

换句话说,a0 = 1.27274,a1 = 0.625272,等等

然后我们可以使用下面的代码来绘制图形。

function decision(x1::Float64, x2::Float64, a::Array{Float64})
    dot(a, [1, x1^1*x2^0, x1^0*x2^1, x1^2*x2^0, x1^1*x2^1, x1^0*x2^2, 
               x1^3*x2^0, x1^2*x2^1, x1^1*x2^2, x1^0*x2^3, x1^4*x2^0, x1^3*x2^1,
               x1^2*x2^2, x1^1*x2^3, x1^0*x2^4, x1^5*x2^0, x1^4*x2^1, x1^3*x2^2,
               x1^2*x2^3, x1^1*x2^4, x1^0*x2^5, x1^6*x2^0, x1^5*x2^1, x1^4*x2^2, 
               x1^3*x2^3, x1^2*x2^4, x1^1*x2^5, x1^0*x2^6])
end

plot(z = (x1,x2) -> decision(x1, x2, para), 
              x = linspace(-1.0, 1.5, 100),
              y = linspace(-1.0, 1.5, 100),
              Geom.contour(levels = [0.0]))

要使 levels = [0.0] 工作,我们需要明确提供参数 x, y, z