Error in setting a for loop

Hello,
I am setting a for loop but I am getting an error:

epsilon = 1          
omega = 0.05      
tau = 2.392731385751533
mu    = 0.72 
p = [epsilon, omega, tau, mu] 
for x in [0.0:0.1:1.0e9]
     println(x)
     y = ε * exp( ω * (ϑ-x) + (ω/μ) * (exp( -μ * (ϑ-x) ) - 1) )
     println(y)
end
0.0:0.1:1.0e9
ERROR: MethodError: no method matching -(::Float64, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}})
For element-wise subtraction, use broadcasting with dot syntax: scalar .- array
Closest candidates are:
  -(::ChainRulesCore.DoesNotExist, ::Any) at /home/gigiux/.julia/packages/ChainRulesCore/UayCG/src/differential_arithmetic.jl:25
  -(::Float64, ::Float64) at float.jl:403
  -(::Float64) at float.jl:393
  ...
Stacktrace:
 [1] top-level scope at ./none:3

Why is x not given as 0.0?
What is the error about?
It looks is a type of varibale error, so I checked the kind of x is generated during the executin:

for x in [0.0:0.1:1.0e9]
               println(typeof(x))
               y = ε * exp( ω * (ϑ-x) + (ω/μ) * (exp( -μ * (ϑ-x) ) - 1) )
               push!(Y, x)
           end
StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}
ERROR: MethodError: no method matching -(::Float64, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}})
For element-wise subtraction, use broadcasting with dot syntax: scalar .- array
Closest candidates are:
  -(::ChainRulesCore.DoesNotExist, ::Any) at /home/gigiux/.julia/packages/ChainRulesCore/UayCG/src/differential_arithmetic.jl:25
  -(::Float64, ::Float64) at float.jl:403
  -(::Float64) at float.jl:393
  ...
Stacktrace:
 [1] top-level scope at ./none:3

It looks like x is not extracted from the generator and also has a type different from the variables, since it should be of type Float64 (or even less since I don’t need so much precision after all).

I also tried to define the iterator with linspace but

?linspace
search: LinSolveGPUFactorize

Couldn't find linspace
Perhaps you meant isspace or isinplace
  No documentation found.

  Binding linspace does not exist.

Is there a new version of it?
Thank you

This is looping on an array that contains a range. You want to loop on the range itself: for x in 0.0:0.1:1.0e9.

4 Likes

I see, it works! Thank you!

1 Like

Yes, it is nowadays called range(start, stop, length=n).

5 Likes