# Array definition error

**URL:** <https://discourse.julialang.org/t/array-definition-error/22181>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 22, 2019, 7:12am UTC](https://discourse.julialang.org/t/array-definition-error/22181 "2019-03-22T07:12:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11111](https://avatars.discourse-cdn.com/v4/letter/1/73ab20/32.png) [@11111](https://discourse.julialang.org/u/11111)\
**Post date:** [March 22, 2019, 7:12am UTC](https://discourse.julialang.org/t/array-definition-error/22181/1 "2019-03-22T07:12:15Z")

</div>

When I wrote the following code, an error occurred in the definition of the array.  
I think that the definition of array u is done, but I do not know why the error occurs.

Changing the definition of [array u] to [global u] did not work well.

```julia
using Plots
gr()
function calc_nrm(a::Matrix{Float64},b::Matrix{Float64})
    n=size(a,1)
    nrm=a-b
    sum=0
    for i in 1:n
        for j in 1:n
            sum+=abs(nrm[i,j]^2)
        end
    end
    ans=sum^0.5

    return ans
end

w=1.
h=1.
Nx=200
Ny=200

delta_x=w/Nx
delta_y=h/Ny

u=zeros(Nx+1,Ny+1)
u_new=zeros(Nx+1,Ny+1)

u[1,:].=0.
u[:,1].=0.
u[Nx+1,:].=40.
u[:,Ny+1].=80.

u_new[1,:].=0.
u_new[:,1].=0.
u_new[Nx+1,:].=40.
u_new[:,Ny+1].=80.

err=0

num=2/(delta_x)^2+2/(delta_y)^2

while err==0
    for j in 2:Nx
        for k in 2:Ny
            u_new[j,k]=(1/num)*((u[j-1,k]+u[j+1,k])/(delta_x^2)+(u[j,k-1]+u[j,k+1])/(delta_y^2))
        end
    end
    if calc_nrm(u,u_new)<2.1
        err=1
    end
    u=u_new
end

```

```julia
ERROR: LoadError: UndefVarError: u not defined
Stacktrace:
 [1] top-level scope at C:\Users\yuki-\Documents\juliapra\wave\laplace.jl:45 [inlined]
 [2] top-level scope at .\none:0
in expression starting at C:\Users\yuki-\Documents\juliapra\wave\laplace.jl:42

```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 22, 2019, 7:28am UTC](https://discourse.julialang.org/t/array-definition-error/22181/2 "2019-03-22T07:28:48Z")

</div>

You need to add a `global` to your `u`:

```julia
    ....
    global u
    u = u_new
end

```

But note that after above statement your `u` and `u_new` point to the same array, which is probably not what you want. Probably best to swap bindings:

```julia
    ....
    global u, u_new
    u, u_new = u_new, u
end

```

Next time, please post a minimal, working (or erroring) example to make it easy on your helpers: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

Also, note that running stuff in global scope, i.e. not in a function is not performant. Check the performance-tips of the docs.

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [March 22, 2019, 8:16am UTC](https://discourse.julialang.org/t/array-definition-error/22181/3 "2019-03-22T08:16:26Z")

</div>

I wrapped the code around the function f1!(u), that modifies the input u. this code works:

```julia
using Plots
gr()
function calc_nrm(a::Matrix{Float64},b::Matrix{Float64})
    n=size(a,1)
    nrm=a-b
    sum=0
    for i in 1:n
        for j in 1:n
            sum+=abs(nrm[i,j]^2)
        end
    end
    ans=sum^0.5

    return ans
end

#wrapped function, with parameters passed as arguments

function f1!(u,params)
w,h,Nx,Ny = params  
delta_x=w/Nx
delta_y=h/Ny
u_new=zeros(Nx+1,Ny+1)

u[1,:].=0.
u[:,1].=0.
u[Nx+1,:].=40.
u[:,Ny+1].=80.

u_new[1,:].=0.
u_new[:,1].=0.
u_new[Nx+1,:].=40.
u_new[:,Ny+1].=80.

err=0

num=2/(delta_x)^2+2/(delta_y)^2

while err==0
    for j in 2:Nx
        for k in 2:Ny
            u_new[j,k]=(1/num)*((u[j-1,k]+u[j+1,k])/(delta_x^2)+(u[j,k-1]+u[j,k+1])/(delta_y^2))
        end
    end
    if calc_nrm(u,u_new)<2.1
        err=1
    end
    u=u_new
    
end
return u
end

#execution

w=1.
h=1.
Nx=200
Ny=200
u=zeros(Nx+1,Ny+1)

params = (w,h,Nx,Ny)
u = f1!(u,params)
```

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [March 22, 2019, 10:08am UTC](https://discourse.julialang.org/t/array-definition-error/22181/4 "2019-03-22T10:08:39Z")

</div>

Yes, in a function, the `global` is not needed (and would be wrong). But the comment about swapping bindings still applies, I think. If you’re from a matlab background, read this [Values vs. Bindings: The Map is Not the Territory · John Myles White](http://www.johnmyleswhite.com/notebook/2014/09/06/values-vs-bindings-the-map-is-not-the-territory/).

---

<div class="post-metadata">

**Author:** ![11111](https://avatars.discourse-cdn.com/v4/letter/1/73ab20/32.png) [@11111](https://discourse.julialang.org/u/11111)\
**Post date:** [March 23, 2019, 9:05am UTC](https://discourse.julialang.org/t/array-definition-error/22181/5 "2019-03-23T09:05:32Z")

</div>

Thanks. It worked!!

I have one more question about swapping array.

```julia
u,u_new=u_new,u

```

I changed this code as follows.

```julia
u=deepcopy(u_new)

```

But, it didn’t work. (It seemed u and u\_new pointed to the same array)

So, is there any ways to do deepcopy() like python??

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 23, 2019, 1:21pm UTC](https://discourse.julialang.org/t/array-definition-error/22181/6 "2019-03-23T13:21:06Z")

</div>

> [@11111](#):
>
> But, it didn’t work. (It seemed u and u\_new pointed to the same array)

It seems that’s what you asked for.

If you are swapping arrays, what is the point of `deepcopy`? But if you insist,

```julia
u, u_new = deepcopy(u_new), deepcopy(u)

```
