# MethodError in For loop

**URL:** <https://discourse.julialang.org/t/methoderror-in-for-loop/75981>\
**Category:** New to Julia\
**Tags:** question, error, loops\
**Created:** [February 7, 2022, 11:15pm UTC](https://discourse.julialang.org/t/methoderror-in-for-loop/75981 "2022-02-07T23:15:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Damiano](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@Damiano](https://discourse.julialang.org/u/Damiano)\
**Post date:** [February 7, 2022, 11:15pm UTC](https://discourse.julialang.org/t/methoderror-in-for-loop/75981/1 "2022-02-07T23:15:50Z")

</div>

This is my first post ever in this forum. I think that I have read all the relevant instructions on how to post. Still, if I am missing something let me know: I will gladly correct myself.

I need to study a system of polynomial equations as a certain parameter `\rho` varies. I wrote the following and it works exactly as I want: result\_block(0.78) gives me the solutions for the value 0.78 of the parameter and then the vector probabilities just keeps the ones I am interested in.

```julia
# Equations for the blocking parameter
using HomotopyContinuation # load the package into the current Julia session
@var x y z; # declare the variables x and y and z
f_block(ρ) = System([x - ρ * z*(z + 2*y*(1-z)),
            y - ρ * (x^2 + 2*x*(1-x)*(y+z) + y*z*(1-4*x+3*x^2)),
            z - ρ * (z^2 + 2*z*(1-z)*(y+x) + y*x*(1-4*z+3*z^2)) ]) # construct system f
result_block(ρ) = real_solutions(solve(f_block(ρ))) # solve f considering only real solutions
B = result_block(0.78)
probabilities = [B[i] for i=1:length(B) if -0.1<B[i][1]<1.1 && -0.1<B[i][2]<1.1 && -0.1<B[i][3]<1.1 && (B[i]!=[0,0,0])]

```

Now I would like to study what happens as `\rho` varies. To do so, I want to plot a graph that is 0 when the probabilities is empty and 1 when probabilities contains some value. I want to use `\rho` on the x-axis and y on the y-axis, where y is defined as described above. To implement such a definition of y I wrote the following:

```julia
y = [1.2]
pop!(y)
for ρ in range(0.6, 0.8, step = 0.01)
    B = result_block(ρ)
    probabilities = [B[i] for i=1:length(B) if -0.1<B[i][1]<1.1 && -0.1<B[i][2]<1.1 && -0.1<B[i][3]<1.1 && (B[i]!=[0,0,0])]
    if probabilities != Any[]
        push!(y,1)
    else
        push!(y,0)
    end
end

```

which does not work and returns the following error:

```julia
MethodError: no method matching +(::Variable, ::Vector{Expression})
For element-wise addition, use broadcasting with dot syntax: scalar .+ array
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at C:\Users\Damiano\AppData\Local\Programs\Julia-1.7.1\share\julia\base\operators.jl:655
  +(::Union{HomotopyContinuation.ModelKit.ExpressionRef, Expression, Variable}) at C:\Users\Damiano\.julia\packages\HomotopyContinuation\UFWkw\src\model_kit\symengine.jl:302
  +(::Union{HomotopyContinuation.ModelKit.ExpressionRef, Expression, Variable}, ::Union{HomotopyContinuation.ModelKit.ExpressionRef, Expression, Variable}) at C:\Users\Damiano\.julia\packages\HomotopyContinuation\UFWkw\src\model_kit\symengine.jl:267
  ...

Stacktrace:
 [1] f_block(ρ::Float64)
   @ Main .\In[1]:4
 [2] result_block(ρ::Float64)
   @ Main .\In[1]:7
 [3] top-level scope
   @ In[3]:4
 [4] eval
   @ .\boot.jl:373 [inlined]
 [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base .\loading.jl:1196

```

One additional piece of information: if I comment out the line `B = result_block(ρ)` I get no error, so I assume that the problem lies there somehow. But to me this line looks so innocent to me!  
I have already spent a few hours trying to solve this issue and I have also tried to google the error and stuff like this.  
If you would be able to help me I would be really grateful!  
Again, I am new to Julia, so I hope that I am not bothering you with something extremely stupid.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [February 8, 2022, 1:18am UTC](https://discourse.julialang.org/t/methoderror-in-for-loop/75981/2 "2022-02-08T01:18:27Z")

</div>

I’m guessing that your problem is that you are using `y` for two different things: as a symbolic variable and as a vector of values to be plotted.

---

<div class="post-metadata">

**Author:** ![Damiano](https://avatars.discourse-cdn.com/v4/letter/d/ebca7d/32.png) [@Damiano](https://discourse.julialang.org/u/Damiano)\
**Post date:** [February 8, 2022, 7:53am UTC](https://discourse.julialang.org/t/methoderror-in-for-loop/75981/3 "2022-02-08T07:53:01Z")

</div>

Oh. Indeed. I have changed the name of y in the for loop. Now it works. I do not know why I did not think of this myself. Maybe in my mind I felt like the whole first block of code was a function, so that the first y was local to it.

Ok, thank you a lot. Now this topic could also be cancelled, so much it is embarrassing!

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [February 8, 2022, 3:52pm UTC](https://discourse.julialang.org/t/methoderror-in-for-loop/75981/4 "2022-02-08T15:52:59Z")

</div>

No worries, we all make mistakes. Yours was nothing compared to some of the bloopers I’ve pulled off!
