我在 Julia 中生成 3D 曲面图时遇到问题(我正在使用 GR 后端)
I'm having trouble generating a 3D surface plot in Julia (I'm using GR backend)
我有一个求解 1D Burguer 方程的程序。我有以下 values/dimensions,这给了我一个尺寸错误。我不明白出了什么问题。如果有人能帮助我,我将不胜感激。
julia> size(xs)
(50,)
julia> size(ts)
(1000,)
julia> size(u)
(50, 1001)
julia> plot(xs,ts,u[1:50,1:1000],st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U")
Arrays have incorrect length or dimension.
有关程序本身的更多信息,以防有帮助
参数(space)
nx= 50;
delta_x = 15/(nx - 1)
x = range(0, stop=delta_x*(nx-1), length=nx) # Full range of spatial steps for which a solution is desired
参数(时间)
endTime = 2 # simulation end time
nt = 1000 # nt is the number of timesteps we want to calculate
delta_t = endTime/nt # Δt is the amount of time each timestep covers
t = range(0, stop=endTime, length=nt) # Full range of time steps for which a solution is desired
#+结果:
: 0.0:0.002002002002002002:2.0
初始条件(space-时间)
# Init array of ones at initial timestep
u_zero = ones(nx)
# Set u₀ = 2 in the interval 0.5 ≤ x ≤ 1 as per our I.C.s
u_zero[0.5 .<= x .<= 3] .= 2 # Note use of . (dot) broadcasting syntax
u_zero
#+结果:
: [1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
运行微分方程
# u[:,] = copy(u_zero) # Initialize arbitrary future timestep with initial condition, u_zero
u=zeros((nx,nt+1))
u[:,1]=copy(u_zero)
for n in 1:nt # loop over timesteps, n: nt times
u[:,n+1] = copy(u[:,n]) # copy the existing values of u^n into u^(n+1)
for i in 2:nx
u[i,n+1] = u[i,n] - u[i,n] * delta_t/delta_x * (u[i,n] - u[i-1,n])
end
end
using Plots
gr()
plot(xs,ts,u[1:50,1:1000],st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U")
评论:
解决方案包括更改 pyplot
的后端并转置 u
矩阵,其中 u'
.
pyplot();
Plots.plot(xs,ts,u'[1:1000,:], st=:surface,title="Burguer equation", xlabel="X", ylabel="Time", zlabel="U")
结果:
(@v1.7) pkg> st Plots
st - Status `~/.julia/environments/v1.7/Project.toml`
[91a5bcdd] Plots v1.25.6
(@v1.7) pkg> st -m GR
Status `~/.julia/environments/v1.7/Manifest.toml`
[28b8d3ca] GR v0.63.1
正如我们在 https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/13 中讨论的那样,目前部分问题似乎与 GR 后端有关。 PyPlot.jl 后端工作正常。
正如评论所暗示的那样,保存 z
坐标的 u
矩阵的维度可能会翻转。一个简单的例子来说明:
julia> x = 1:5
1:5
julia> y = 1:10
1:10
julia> z = [x+y for x in 1:5, y in 1:10]
5×10 Matrix{Int64}:
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
julia> surface(x, y, z)
Arrays have incorrect length or dimension.
julia> surface(x, y, z')
并且最后一个命令给出了正确的图(在这个简单的示例中,很容易验证 z
对于 x
= 5 和 y
= 10 确实应该是 15):
或者,您可以将三个向量传递给 surface
,这在文档 here 中有所描述,但需要先从 x 和 y 向量构建网格:
f(x, a) = begin
1 / x + a * x ^ 2
end
xs = collect(0.1:0.05:2.0)
as = collect(0.2:0.1:2.0)
x_grid = [x for x = xs for y = as]
a_grid = [y for x = xs for y = as]
surface(x_grid, a_grid, f.(x_grid, a_grid))
我有一个求解 1D Burguer 方程的程序。我有以下 values/dimensions,这给了我一个尺寸错误。我不明白出了什么问题。如果有人能帮助我,我将不胜感激。
julia> size(xs)
(50,)
julia> size(ts)
(1000,)
julia> size(u)
(50, 1001)
julia> plot(xs,ts,u[1:50,1:1000],st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U")
Arrays have incorrect length or dimension.
有关程序本身的更多信息,以防有帮助
参数(space)
nx= 50;
delta_x = 15/(nx - 1)
x = range(0, stop=delta_x*(nx-1), length=nx) # Full range of spatial steps for which a solution is desired
参数(时间)
endTime = 2 # simulation end time
nt = 1000 # nt is the number of timesteps we want to calculate
delta_t = endTime/nt # Δt is the amount of time each timestep covers
t = range(0, stop=endTime, length=nt) # Full range of time steps for which a solution is desired
#+结果: : 0.0:0.002002002002002002:2.0
初始条件(space-时间)
# Init array of ones at initial timestep
u_zero = ones(nx)
# Set u₀ = 2 in the interval 0.5 ≤ x ≤ 1 as per our I.C.s
u_zero[0.5 .<= x .<= 3] .= 2 # Note use of . (dot) broadcasting syntax
u_zero
#+结果: : [1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
运行微分方程
# u[:,] = copy(u_zero) # Initialize arbitrary future timestep with initial condition, u_zero
u=zeros((nx,nt+1))
u[:,1]=copy(u_zero)
for n in 1:nt # loop over timesteps, n: nt times
u[:,n+1] = copy(u[:,n]) # copy the existing values of u^n into u^(n+1)
for i in 2:nx
u[i,n+1] = u[i,n] - u[i,n] * delta_t/delta_x * (u[i,n] - u[i-1,n])
end
end
using Plots
gr()
plot(xs,ts,u[1:50,1:1000],st=:surface, title="Burguer equation", xlabel="X", ylabel="Y", zlabel="U")
评论:
解决方案包括更改 pyplot
的后端并转置 u
矩阵,其中 u'
.
pyplot();
Plots.plot(xs,ts,u'[1:1000,:], st=:surface,title="Burguer equation", xlabel="X", ylabel="Time", zlabel="U")
结果:
(@v1.7) pkg> st Plots
st - Status `~/.julia/environments/v1.7/Project.toml`
[91a5bcdd] Plots v1.25.6
(@v1.7) pkg> st -m GR
Status `~/.julia/environments/v1.7/Manifest.toml`
[28b8d3ca] GR v0.63.1
正如我们在 https://discourse.julialang.org/t/unmatched-dimensions-for-3d-plot/75180/13 中讨论的那样,目前部分问题似乎与 GR 后端有关。 PyPlot.jl 后端工作正常。
正如评论所暗示的那样,保存 z
坐标的 u
矩阵的维度可能会翻转。一个简单的例子来说明:
julia> x = 1:5
1:5
julia> y = 1:10
1:10
julia> z = [x+y for x in 1:5, y in 1:10]
5×10 Matrix{Int64}:
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
julia> surface(x, y, z)
Arrays have incorrect length or dimension.
julia> surface(x, y, z')
并且最后一个命令给出了正确的图(在这个简单的示例中,很容易验证 z
对于 x
= 5 和 y
= 10 确实应该是 15):
或者,您可以将三个向量传递给 surface
,这在文档 here 中有所描述,但需要先从 x 和 y 向量构建网格:
f(x, a) = begin
1 / x + a * x ^ 2
end
xs = collect(0.1:0.05:2.0)
as = collect(0.2:0.1:2.0)
x_grid = [x for x = xs for y = as]
a_grid = [y for x = xs for y = as]
surface(x_grid, a_grid, f.(x_grid, a_grid))