MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type Float64 in the ODEProblem

I have been trying to solve this differential equation using ODEProblem whose code is given below,

using QuadGK
function solve_eq(a0::Float64)
    e0 = sqrt(2/(8.85*10^(-12)*3*10^8) * (0.5 * 10^(12)/(10^-4)))
    e_field(t) = e0 * cos(2.35*(t) + 0.003*(t)^2)*exp(-(t)^2/25.0^2)
    vector_potential(t) = -1 * quadgk(x-> e_field(x), -200.0, t)[1]


    k_time(t) = 1.6e-19*a0/(1.054571817e-34) * 1e-15 * vector_potential(t)

    function pulse_tdse!(du, u, p, t)
        du[1] = k_time(t) * u[1]
        du[2] = k_time(t) * u[2]
    end
    # a0 = [exp(1im*pi/4), exp(1im*pi/4)]
    a0 = [0.0, 0.0]
    tspan = (-100.0, 100.0)
    prob = ODEProblem(pulse_tdse!, a0, tspan)
    sol = solve(prob, RK4(), dt = 0.1, saveat = 0.1)
    return sol
end

solve_eq(4.2e-10)

This gives an error

MethodError: Cannot `convert` an object of type Vector{Float64} to an object of type Float64

However, I was able to identifiy the problem, if I remove the argument of the solve_eq function then it runs smoothly. The debugged code is below,


using QuadGK
function solve_eq()
    e0 = sqrt(2/(8.85*10^(-12)*3*10^8) * (0.5 * 10^(12)/(10^-4)))
    e_field(t) = e0 * cos(2.35*(t) + 0.003*(t)^2)*exp(-(t)^2/25.0^2)
    vector_potential(t) = -1 * quadgk(x-> e_field(x), -200.0, t)[1]


    k_time(t) = 1.6e-19*4.2e-10/(1.054571817e-34) * 1e-15 * vector_potential(t)

    function pulse_tdse!(du, u, p, t)
        du[1] = k_time(t) * u[1]
        du[2] = k_time(t) * u[2]
    end
    # a0 = [exp(1im*pi/4), exp(1im*pi/4)]
    a0 = [0.0, 0.0]
    tspan = (-100.0, 100.0)
    prob = ODEProblem(pulse_tdse!, a0, tspan)
    sol = solve(prob, RK4(), dt = 0.1, saveat = 0.1)
    return sol
end

solve_eq()

Can someone let me know what is the problem with the first code?

I figured out the problem. The a0 argument and the initial value variable name is same. That’s why its creating problem. The following code works perfectly fine.

using QuadGK
function solve_eq(a0::Float64)
    e0 = sqrt(2/(8.85*10^(-12)*3*10^8) * (0.5 * 10^(12)/(10^-4)))
    e_field(t) = e0 * cos(2.35*(t) + 0.003*(t)^2)*exp(-(t)^2/25.0^2)
    vector_potential(t) = -1 * quadgk(x-> e_field(x), -200.0, t)[1]


    k_time(t) = 1.6e-19*a0/(1.054571817e-34) * 1e-15 * vector_potential(t)

    function pulse_tdse!(du, u, p, t)
        du[1] = k_time(t) * u[1]
        du[2] = k_time(t) * u[2]
    end
    # a0 = [exp(1im*pi/4), exp(1im*pi/4)]
    u0 = [0.0, 0.0]
    tspan = (-100.0, 100.0)
    prob = ODEProblem(pulse_tdse!, u0, tspan)
    sol = solve(prob, RK4(), dt = 0.1, saveat = 0.1)
    return sol
end

solve_eq(4.2e-10)

this would also work fine if you just remove the type constraint on a0 which doesn’t help performance at all.