# ERROR: MethodError: no method matching -(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Float64)

**URL:** <https://discourse.julialang.org/t/error-methoderror-no-method-matching-steprangelen-float64-base-twiceprecision-float64-base-twiceprecision-float64-float64/41993>\
**Category:** General Usage\
**Created:** [June 24, 2020, 3:54pm UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-steprangelen-float64-base-twiceprecision-float64-base-twiceprecision-float64-float64/41993 "2020-06-24T15:54:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hafez\_ahmad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hafez_ahmad/32/14423_2.png) [@hafez\_ahmad](https://discourse.julialang.org/u/hafez_ahmad)\
**Post date:** [June 24, 2020, 3:54pm UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-steprangelen-float64-base-twiceprecision-float64-base-twiceprecision-float64-float64/41993/1 "2020-06-24T15:54:36Z")

</div>

why I am getting error? any help appreciated  
T=0.5;

# Parameters of ODE system

a=[N, x0 ,xN ,c];

x=range(x0,step=xN,stop=N);

# Exact solution

uexc=exp.(-(x-T-2).\*(x-T-2));

ERROR: MethodError: no method matching -(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Float64)  
Closest candidates are:  
-(::Float64, ::Float64) at float.jl:403  
-(::Complex{Bool}, ::Real) at complex.jl:307  
-(::Missing, ::Number) at missing.jl:115  
…  
Stacktrace:  
[1] top-level scope at none:0

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 24, 2020, 3:57pm UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-steprangelen-float64-base-twiceprecision-float64-base-twiceprecision-float64-float64/41993/2 "2020-06-24T15:57:45Z")

</div>

The problem is

```julia
x-T

```

here, `x` is a range, so you need to broadcast your subtraction like

```julia
x .- T

```

as you’re trying to subtract `T` from every element of `x`, not from `x` itself. The error tells you this - there is no method for subtracting a `Float64` number from a `StepRangeLen` object.

Incidentally, please make sure to quote your code by using triple backticks ``` to make it easier to read.

---

<div class="post-metadata">

**Author:** ![hafez\_ahmad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hafez_ahmad/32/14423_2.png) [@hafez\_ahmad](https://discourse.julialang.org/u/hafez_ahmad)\
**Post date:** [June 24, 2020, 4:45pm UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-steprangelen-float64-base-twiceprecision-float64-base-twiceprecision-float64-float64/41993/3 "2020-06-24T16:45:15Z")

</div>

would you please check it , I am still getting error  
using PyPlot  
using DifferentialEquations  
function rhs(dudt,u,A::Array{Float64,1},t)  
N=Integer(A[1]);x0=A[2];xN=A[3]; c=A[4];  
x=range(x0,step=xN,stop=N);  
dx=x[2]-x[1];  
# Periodic boundary condition  
dudt[1]=-c\*(u[1]-u[N])/dx;  
for n in range(2,N-1)  
dudt[n] =- c\*(u[n]-u[n-1])/dx;  
end  
end

N=100;x0=0.;xN=10.;c=1.;  
T=0.5;

# Parameters of ODE system

A=[N, x0 ,xN ,c];

x=range(x0,step=xN,stop=N);

# Exact solution

uexc=exp.(-(x.-T.-2).\*(x.-T.-2));

# Initial value

u0=exp.(-(x.-2).\*(x.-2));

# Problem definition of ODE in Julia

tspan = (0.0,0.5)

prob = ODEProblem(rhs,u0,tspan,A)

# Solving ODE

sol = solve(prob,saveat=0.5)  
gcf()  
PyPlot.plot(x,u0,label=“Initial value”)  
PyPlot.plot(x,sol[end],label=string("Solution at t= ",T))  
PyPlot.plot(x,uexc,label=string("Exact solution at t= ",T))  
PyPlot.legend()  
PyPlot.grid(linestyle=“dotted”)  
PyPlot.xlabel(“x”)  
PyPlot.ylabel(“u”)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 24, 2020, 4:48pm UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-steprangelen-float64-base-twiceprecision-float64-base-twiceprecision-float64-float64/41993/4 "2020-06-24T16:48:01Z")

</div>

Unfortunately I don’t have time to install all the required packages to check what is going wrong here - what is the error you’re getting? Can you reduce your post down to the area that is causing the actual problem?

Also, did you read my suggestion above about quoting your code in triple backticks (```) to make it more legible?

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [June 24, 2020, 5:13pm UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-steprangelen-float64-base-twiceprecision-float64-base-twiceprecision-float64-float64/41993/5 "2020-06-24T17:13:38Z")

</div>

In the `rhs` function, you are indexing the 11-element array `u` at index `N==100`. Try something like this:

```julia
function rhs(dudt,u,A::Array{Float64,1},t)
              N=Integer(A[1]);x0=A[2];xN=A[3]; c=A[4];
              x=range(x0,step=xN,stop=N);
              dx=x[2]-x[1];
              # Periodic boundary condition
              dudt[1]=-c*(first(u)-last(u))/dx;
              for n in 2:length(u)
                  dudt[n] =- c*(u[n]-u[n-1])/dx;
              end
        end

```

---

<div class="post-metadata">

**Author:** ![hafez\_ahmad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hafez_ahmad/32/14423_2.png) [@hafez\_ahmad](https://discourse.julialang.org/u/hafez_ahmad)\
**Post date:** [June 25, 2020, 2:35am UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-steprangelen-float64-base-twiceprecision-float64-base-twiceprecision-float64-float64/41993/6 "2020-06-25T02:35:24Z")

</div>

thank you

---

<div class="post-metadata">

**Author:** ![hafez\_ahmad](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hafez_ahmad/32/14423_2.png) [@hafez\_ahmad](https://discourse.julialang.org/u/hafez_ahmad)\
**Post date:** [June 25, 2020, 2:35am UTC](https://discourse.julialang.org/t/error-methoderror-no-method-matching-steprangelen-float64-base-twiceprecision-float64-base-twiceprecision-float64-float64/41993/7 "2020-06-25T02:35:45Z")

</div>

thank you , it worked
