ArielFuentes
ArielFuentes

Reputation: 23

Using == in a function

I'm getting problems with a function. This function is supossed to evaluate some values on an array and, if an equality gets true, then, it should return that value. It's not working. Here´s my code.

λ = 650*10^-9     # Longitud de onda
b = 2*10^-3     #Ancho de una rendija
k = 2π/λ     #Constante
n = 1     #Número de rendijas
m = -10:10
max = []
Trash = []
o = -1:0.01:1

function Maximo(A,B,C)
    tan(B*sin(A)*C/2) == B*sin(A)*C/2

    if true
        push!(max,A)
    else 
        push!(Trash, A)
    end
end

Notice and excuse my poor English and my programming skills.

I just want to get the values of "o" for which the equality is true.' I don't care about "Trash"; it's just and array I´ve made in my desperation.

Then, when I call

Maximo.(o,k,b)

It returns me a 201-element Array{Array{Any,1},1}

Could you give me a hand, please?

Upvotes: 0

Views: 67

Answers (1)

phipsgabler
phipsgabler

Reputation: 20950

I don't quite understand your requirements, but I think this should be good?

function maximos(A, b, c)
    # make a new array of the same type as A, but with size 0
    result = similar(A, 0)  

    for a in A
        if tan(b * sin(a) * c/2) == b * sin(a) * c/2
            push!(result, a)
        end
    end

    return result
end

Note that you need to call it as max = maximos(o, k, b), although you shouldn't name the result max, since that is already an existing function name in the standard library.

If you get familiar with higher order functions, you can start writing things like

maximos(A, b, c) = filter(A) do a
    tan(b * sin(a) * c/2) == b * sin(a) * c/2
end

Upvotes: 1

Related Questions