# Variable not found

**URL:** <https://discourse.julialang.org/t/variable-not-found/111865>\
**Category:** New to Julia\
**Created:** [March 20, 2024, 10:59am UTC](https://discourse.julialang.org/t/variable-not-found/111865 "2024-03-20T10:59:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mishal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mishal/32/214462_2.png) [@Mishal](https://discourse.julialang.org/u/Mishal)\
**Post date:** [March 20, 2024, 10:59am UTC](https://discourse.julialang.org/t/variable-not-found/111865/1 "2024-03-20T10:59:47Z")

</div>

Hi Guys

Please refer to the below code. It was running perfectly and now for some reason I am getting the following error, “ERROR: UndefVarError: `sol` not defined”. Which makes no sense it has been defined…

Any assistance would be appreciated.

```julia
using Plots
using DifferentialEquations

function sdof!(dx,x,p,t)
    m,k,c,f,w = p
    dx[1] = x[2]
    dx[2] = (-c*x[2]-k*x[1]+f*sin(w*t))/m
end

nf = [9.69,57.77,127.28,174.53,313.719]*6.28 # [rad/s]
m = [74.11,13.241,2.4,3.4,2.366]/100# [%]

f = 1000 # [N] forcing function
M = 611.778

tspan = (0,1)
x0 = [0.00,0.00]
T_displacement = zeros(length(tspan[1]:0.001:tspan[2]))

for i in 1:length(nf)
    k = nf[i]^2*m[i]*M
    c = 0.01*(m[i]*M+k)
    p = m[i]*M,k,c,f,50*6.28
    prob = ODEProblem(sdof!, x0, tspan, p)
    sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8, saveat = 0.001)
    T_displacement += sol[1,:]
end

plotly()

#plt_1 = plot()
display(plot(sol.t,T_displacement,xlabel = "Time (sec)", ylabel = "Displacement (m)"))

```

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [March 20, 2024, 11:08am UTC](https://discourse.julialang.org/t/variable-not-found/111865/2 "2024-03-20T11:08:10Z")

</div>

`sol` is a local variable and is not defined in the global scope, see [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#scope-of-variables)

Use `global sol = solve(...)`.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [March 20, 2024, 11:08am UTC](https://discourse.julialang.org/t/variable-not-found/111865/3 "2024-03-20T11:08:18Z")

</div>

If you define a variable inside a loop, it’s not available outside the loop:

```julia
julia> for i in 1:10
           x = 1
       end

julia> x
ERROR: UndefVarError: `x` not defined

```

The solution is to add `global`:

```julia
julia> for i in 1:10
           global x = 1
       end

julia> x
1

```

It’s better style to put code into functions - it’ll run faster, and be more debuggable. If you do anything bigger than a throwaway script, or to quickly test something, I recommend doing that.

---

<div class="post-metadata">

**Author:** ![Mishal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mishal/32/214462_2.png) [@Mishal](https://discourse.julialang.org/u/Mishal)\
**Post date:** [March 25, 2024, 7:55am UTC](https://discourse.julialang.org/t/variable-not-found/111865/4 "2024-03-25T07:55:45Z")

</div>

Hi Guys

Thanks for the replies, I just figured out the issue a few minutes after posting.
