何时使用转置法在Julia中绘制轮廓

问题描述 投票:2回答:1

因此,我尝试使用以下代码通过插值2D函数在Julia中绘制轮廓:

using Interpolations
using Plots
gr()

xs = 1:0.5:5
ys = 1:0.5:8
# The function to be plotted
f(x, y) = (3x + y ^ 2)
g = Float64[f(x,y) for x in xs, y in ys]

# Interpolate the function
g_int = interpolate(g, BSpline(Quadratic(Line(OnCell()))))

# Scale the interpolated function to the correct grid 
gs_int = scale(g_int, xs, ys)

xc = 1:0.1:5
yc = 1:0.1:5

# Compare the real value and the interpolated value of the function at an arbitrary point
println("gs_int(3.2, 3.2) = ", gs_int(3.2, 3.2))
println("f(3.2, 3.2) = ", f(3.2, 3.2))

# Contour of the interpolated plot
p1 = contour(xs, ys, gs_int(xs, ys), fill=true)
# Real contour of the function
p2 = contour(xc, yc, f, fill=true)

plot(p1, p2)

尽管显然内插是正确的,但这显然没有给出正确的轮廓:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS9idDQzclZnLnBuZyJ9” alt =“ a”>

此问题已通过移调gs_int(xs, ys)来解决:

p1 = contour(xs, ys, gs_int(xs, ys)', fill=true)

然后我在2D空间中随机生成一些点,并重复相同的过程:

using DelimitedFiles
using Interpolations
using Plots
gr()

data = readdlm("./random_points.txt", Float64)

# Create a dictionary to test different orders of interpolations. 
inter = Dict("constant" => BSpline(Constant()), 
    "linear" => BSpline(Linear()), 
    "quadratic" => BSpline(Quadratic(Line(OnCell()))),
    "cubic" => BSpline(Cubic(Line(OnCell())))
)

x = range(-10, length=64, stop=10)
y = range(-10, length=64, stop=10)

v_unscaled = interpolate(data, inter["cubic"])
v = scale(v_unscaled, x, y)

# The contour of the data points
p0 = contour(x, y, data, fill=true)
display(p0)

# The contour of the interpolated function
p_int = contour(x, y, v(x,y)', fill=true)
display(p_int)

但是,两个等高线图看起来并不相同。

“

[我在v(x,y)之后删除了单引号,这起作用了:

p_int = contour(x, y, v(x,y), fill=true)

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLmltZ3VyLmNvbS8wYUxSc3NTLnBuZyJ9” alt =“ c”>

现在我不明白。我什么时候应该应用换位,什么时候不应该使用?

arrays plot julia interpolation contour
1个回答
4
投票

这是因为在第一个示例中绘制了一个函数,在第二个示例中绘制了两个数组。这两个数组的方向相同,因此无需进行转置。但是在第一个示例中,生成数组的方式相对于Plots从所传递的二维函数生成数组的方式进行了转换。

[绘制函数时,绘图将以g = Float64[f(x,y) for y in ys, x in xs]计算结果,而不是相反,就像您在代码中所做的那样。要更好地讨论绘图中的转置,请再次参考https://github.com/JuliaPlots/Makie.jl/issues/205

© www.soinside.com 2019 - 2024. All rights reserved.