这里是 Julia 新手! (请对我宽容一点,原谅我糟糕的英语和我的编程技巧)。
我正在开发 Julia (1.3.1) 项目。 (我正在使用 Jupyter)。 这是关于光学的;许多狭缝的衍射。 (使用夫琅和费衍射)。 我想要一个非常酷的图来显示衍射图案。 我快到了,但我的情节有问题,它弹出下一条消息: 当我尝试绘制它时,“MethodError:没有方法匹配/(::Int64,::typeof(r))”。
这是我的代码。
using Pkg
Pkg.add("Plots")
Pkg.add("Plotly")
Pkg.add("Interact")
using Plots
plotly()
theme(:juno)
ϵ= 8.8541878176* 10^-12
a = 1 #Distancia del centro de una rendija al centro de la siguiente
b = 1 #Ancho de una rendija
k = 1 #Constante
R = 1 #Distancia de la rendija central a la fuente
n = 1 #Número de rendijas
P = a*(n/2)+b/2 #Calcula la "altura" de la fuente
p= P:-0.1:-P #Determina la altura de incidencia de los rayos
function r(p) #Calcula la distancia de la fuente a un punto de la rendija
sqrt(R^2+p^2)
end
function Θ(r)
acosd(R/r)
end
@.α = (a*k*sind(Θ(r(p))))/2
@.β = (b*k*sind(Θ(r(p))))/2
function Ir(d, Α, Β)
(1/2)*(ϵ*b/d)^(2)*sinc(Β)^(2)*((sind(n*Α)^2)/(sind(Α)^2))
end
plot.(Θ(r),Ir(r(p),α,β))
我在每一行都迈出了一小步;我打印结果是为了确保它按照我想要的方式“工作”。 在最后一行(“plot.(θ(r),Ir(r(p),α,β))”行)中,它显示下一条消息;
MethodError: no method matching /(::Int64, ::typeof(r))
Closest candidates are:
/(::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, !Matched::Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) at int.jl:59
/(::T, !Matched::T) where T<:Integer at int.jl:57
/(::R, !Matched::S) where {R<:Real, S<:Complex} at complex.jl:328
...
Stacktrace:
[1] Θ(::Function) at .\In[218]:2
[2] top-level scope at In[306]:1
顺便问一下,对于一个很酷的情节有什么建议吗? 我将使用(如果我的代码有效)“interact”包,这个包可以帮助我让用户更改狭缝的数量和其他参数吗?
非常感谢你们,伙计们。作为一名 super 新手程序员,我非常感谢您的建议。
最佳答案
嗯,您刚刚将 r 定义为一个函数。在 Julia 中,函数的类型显示为 typeof(r)
。然后在最后一行中,执行 θ(r)
,即 R/r。但由于 r 是一个函数,因此没有任何意义(如何将整数除以函数)?
关于jupyter-notebook - MethodError:没有方法匹配/(::Int64,::typeof(r)) [Julia],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61711297/