MethodError in For loop

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.

# 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:

y = [1.2]
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[]

which does not work and returns the following error:

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

 [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.

1 Like

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.


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!

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