# Getting UndefVarError even though variables are defined

**URL:** <https://discourse.julialang.org/t/getting-undefvarerror-even-though-variables-are-defined/89383>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 27, 2022, 4:10pm UTC](https://discourse.julialang.org/t/getting-undefvarerror-even-though-variables-are-defined/89383 "2022-10-27T16:10:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/max_a/32/43905_2.png) [@Max\_A](https://discourse.julialang.org/u/Max_A)\
**Post date:** [October 27, 2022, 4:10pm UTC](https://discourse.julialang.org/t/getting-undefvarerror-even-though-variables-are-defined/89383/1 "2022-10-27T16:10:19Z")

</div>

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?

```julia
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`

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 27, 2022, 4:54pm UTC](https://discourse.julialang.org/t/getting-undefvarerror-even-though-variables-are-defined/89383/2 "2022-10-27T16:54:48Z")

</div>

> [@Max\_A](#):
>
> ` rhs!(du, u, p, t)`

you need to define `p` before this line

---

<div class="post-metadata">

**Author:** ![Max\_A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/max_a/32/43905_2.png) [@Max\_A](https://discourse.julialang.org/u/Max_A)\
**Post date:** [October 27, 2022, 5:16pm UTC](https://discourse.julialang.org/t/getting-undefvarerror-even-though-variables-are-defined/89383/3 "2022-10-27T17:16:27Z")

</div>

Even when I define p I still get the error

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 27, 2022, 5:27pm UTC](https://discourse.julialang.org/t/getting-undefvarerror-even-though-variables-are-defined/89383/4 "2022-10-27T17:27:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Max\_A](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/max_a/32/43905_2.png) [@Max\_A](https://discourse.julialang.org/u/Max_A)\
**Post date:** [October 27, 2022, 5:30pm UTC](https://discourse.julialang.org/t/getting-undefvarerror-even-though-variables-are-defined/89383/5 "2022-10-27T17:30:14Z")

</div>

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

```julia
ERROR: LoadError: UndefVarError: t not defined

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [October 27, 2022, 5:45pm UTC](https://discourse.julialang.org/t/getting-undefvarerror-even-though-variables-are-defined/89383/6 "2022-10-27T17:45:33Z")

</div>

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()`) .
