【发布时间】:2016-07-12 19:55:57
【问题描述】:
我正在尝试在 Julia 中使用 pyplot 绘制两个变量的函数。工作起点如下(可在 StackOverflow 找到):
function f(z,t)
return z*t
end
z = linspace(0,5,11)
t = linspace(0,40,4)
for tval in t
plot(z, f(z, tval))
end
show()
这对我很有效,并且给了我想要的东西: a field of lines.
我自己的功能如下:
## needed functions ##
const gamma_0 = 6
const Ksch = 1.2
const Kver = 1.5
function Kvc(vc)
if vc <= 0
return 0
elseif vc < 20
return (100/vc)^0.1
elseif vc < 100
return 2.023/(vc^0.153)
elseif vc == 100
return 1
elseif vc > 100
return 1.380/(vc^0.07)
else
return 0
end
end
function Kgamma(gamma_t)
return 1-((gamma_t-gamma_0)/100)
end
function K(gamma_t, vc)
return Kvc(vc)*Kgamma(gamma_t)*Ksch*Kver
end
我尝试将它们绘制如下:
i = linspace(0,45,10)
j = linspace(0,200,10)
for i_val in i
plot(i,K(i,j))
end
这给了我以下错误:
isless没有方法匹配 isless(::Int64, ::Array{Float64,1}) 加载 In[51] 时,从第 3 行开始的表达式中在 In[17] 的 Kvc 中:2 在无文件的匿名中:4
显然,我的函数无法处理数组。
下一次尝试:
i = linspace(0,200,11)
j = linspace(0,45,11)
for i_val in i
plot(i_val,map(K,i_val,j))
end
给我一个只有轴的空图
谁能给我一个提示...
编辑
一个更简单的例子:
using PyPlot
function P(n,M)
return (M*n^3)/9550
end
M = linspace(1,5,5)
n = linspace(0,3000,3001)
for M_val in M
plot(n,P(n,M_val))
end
show()
解决方案
好的,在您的帮助下,我找到了适用于我的缩短示例的解决方案:
function P(n,M)
result = Array(Float64, length(n))
for (idx, val) in enumerate(n)
result[idx] = (M*val^3)/9550
end
return result
end
n = linspace(0,3000,3001)
for M_val = 1:5
plot(n,P(n,M_val))
end
show()
这给了我what I wanted for this shortened example。剩下的问题是:能否以更简单更优雅的方式完成?
我会尝试将其应用于原始示例并在成功时发布。
【问题讨论】:
标签: matplotlib plot julia