Getting UndefVarError even though variables are defined

Hi all,

I am using the following code to solve an ODE. I defined my variables but I am still getting an UndefVarError.

Any help here?

v_x(x, y, t) = -sin(pi * y) * cos(pi * t / 5)
v_y(x, y, t) =  sin(pi * x) * cos(pi * t / 5)
p = 0
# we assume u is a 2 x num_particles matrix
function rhs!(du, u, p, t) 
    num_particles = size(u, 2)
    for i in 1:num_particles
        du[1, i] = v_x(u[1, i], u[2, i], t)
        du[2, i] = v_y(u[1, i], u[2, i], t)
    end
end

# modify dt so that we take exactly Nsteps to get to the final time
FinalTime = 5
dt = .001
Nsteps = ceil(Int, FinalTime / dt)
dt = FinalTime / Nsteps

num_particles = 100
u = 0.1 .+ .45 * rand(2, num_particles)

u0 = copy(u) # to compare to the solution at the final time
du = similar(u)
t = 0.0
for i = 1:Nsteps
    rhs!(du, u, p, t)
    # the following loops are also equivalent to "@. u = u + dt * du"
    for j = 1:size(u, 2)
        for i = 1:size(u,1)
            u[i,j] = u[i,j] + dt * du[i,j]
        end
    end
    t = t + dt
end

# Plot the initial configuration to
scatter(u0[1,:], u0[2,:], label="Initial particle positions")
scatter!(u[1,:], u[2,:], label="Final particle positions")
plot!(xlims=(-1,1), ylims=(-1,1))

I am getting the following error
ERROR: LoadError: UndefVarError: t not defined

you need to define p before this line

Even when I define p I still get the error

Post what you ate running, because I don’t.

I added p = 0 as you suggested. I am now getting this error:

ERROR: LoadError: UndefVarError: t not defined

Probably this is a global related problem. Just put all of your code inside a single function and call this function (e.g., function main() ... all your code .. end; main()) .