MethodError: no method matching /(::Int64, ::typeof(r)) [Julia]

Viewed 719

A Julia-newby here! (Please, go easy on me and excuse my poor english and my programming skills).

I´m working on a Julia (1.3.1) project. (I am using Jupyter). It is about optics; diffraction by many slits. (Using Fraunhofer diffraction). I would like to get a quite cool plot showing diffraction patterns. I´m almost there, but there is a problem with my plot, it pops up the next message: "MethodError: no method matching /(::Int64, ::typeof(r))" when I try to plot it.

Here is my code.


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),α,β))

I´m taking baby steps in each line; I´m printing results in order to make sure it "works" the way I would like to. In the last line, (that "plot.(Θ(r),Ir(r(p),α,β))" line), it shows up the next message;

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

By the way, any piece of advice for a cool plotting? I am going to use (if my code ever works) "interact" package, could this package help me to let the users change the number of slits and other parameters?

Thank you so much, guys. As a ultra-newby programmer, I REALLY aprecciate your pieces of advice.

2 Answers

Well, you've just defined r to be a function. In Julia, the type of a function is displayed like typeof(r). Then in your last line, you do Θ(r), which the does R/r. But since r is a function, that makes no sense (how do you divide an integer by a function)?

What the error actually means is that there in function Θ() you are giving a function as an argument and doing calculations with it which makes no sense to the interpreter. You can see this in the lines:

Stacktrace:
 [1] Θ(::Function) at .\In[218]:2
 [2] top-level scope at In[306]:1

Here you passed 'r' as an argument to Θ, and because r is a function (defined by you earlier in the code). You can fix this by providing arguments to r inside of Θ something like this:

Θ(r(p))

if that's what you want to do, or you can change the name of the function or use different variables if you don't want to pass function r(p) as an argument. Hope this helps.

Related