# Variables interfering with each other

**URL:** <https://discourse.julialang.org/t/variables-interfering-with-each-other/62143>\
**Category:** New to Julia\
**Created:** [May 31, 2021, 4:09pm UTC](https://discourse.julialang.org/t/variables-interfering-with-each-other/62143 "2021-05-31T16:09:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [May 31, 2021, 4:09pm UTC](https://discourse.julialang.org/t/variables-interfering-with-each-other/62143/1 "2021-05-31T16:09:34Z")

</div>

Dear all,  
I am implementing the divided differences of Newton’ polynomial. However a strange thing happens.  
I define the x and y values by X and Y variables. Inside the function I define the vector \Delta Y = Y.  
But, at the end of the execution, I miss the Y value (because \Delta Y changes).  
How can I fix this?  
Below, my code:

```julia
function Diferencas_Divididas(X,Y)
    n=length(X)
    Δy = Y
    a = zeros(n)
    a[1] = Y[1]

    for i in 1:n-1
        for j in 1:n-i
            Δy[j] = (Δy[j+1] - Δy[j]) / (X[j+i] - X[j])
        end
        a[i+1] = Δy[1]
    end
    return a
end

X = [2 2.5 3 3.5]
Y = [7.4 12.2 20.1 33.4]
n=length(X)

a = Diferencas_Divididas(X,Y)

julia> Y
1×4 Array{Float64,2}:
 3.06667 10.8 26.6 33.4

julia> 
´´´
```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 31, 2021, 4:13pm UTC](https://discourse.julialang.org/t/variables-interfering-with-each-other/62143/2 "2021-05-31T16:13:44Z")

</div>

You probably want `Δy = copy(Y)`. Otherwise `Δy` and `Y` will refer to the same object.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 31, 2021, 4:13pm UTC](https://discourse.julialang.org/t/variables-interfering-with-each-other/62143/3 "2021-05-31T16:13:59Z")

</div>

You need `Δy = copy(Y)`

then

```julia
julia> a = Diferencas_Divididas(X,Y)
4-element Vector{Float64}:
 7.4
 9.599999999999998
 6.200000000000006
 3.0666666666666558

julia> Y
1×4 Matrix{Float64}:
 7.4 12.2 20.1 33.4

```
