# ODE: no method matching similar

**URL:** https://discourse.julialang.org/t/ode-no-method-matching-similar/40764
**Category:** General Usage
**Tags:** diffeq
**Created:** [June 4, 2020, 8:55pm UTC](https://discourse.julialang.org/t/ode-no-method-matching-similar/40764 "2020-06-04T20:55:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![99dB](https://avatars.discourse-cdn.com/v4/letter/9/838e76/32.png) [@99dB](https://discourse.julialang.org/u/99dB)
#### Post date: [June 4, 2020, 8:55pm UTC](https://discourse.julialang.org/t/ode-no-method-matching-similar/40764/1 "2020-06-04T20:55:27Z")

</div>

Hi All,

I try to compile an ODE model of 1 variable u but I need an if statement based on t.

like something:

```julia
function dudt!(du,u,p,t)
if rem(t,10) < 5
pp = p[1]*t;
end
du = pp*u
end

```

But I get the following message:

```julia
MethodError: no method matching similar(::Float64)

```

Do you have any ideas?  
Many thanks!

---

<div class="post-metadata">

### Author: ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)
#### Post date: [June 4, 2020, 9:05pm UTC](https://discourse.julialang.org/t/ode-no-method-matching-similar/40764/2 "2020-06-04T21:05:47Z")

</div>

Not sure if this will fix it because I don’t have the whole picture, but change `du = pp*u` to `du .= pp*u` for updating `du` in-place.

---

<div class="post-metadata">

### Author: ![99dB](https://avatars.discourse-cdn.com/v4/letter/9/838e76/32.png) [@99dB](https://discourse.julialang.org/u/99dB)
#### Post date: [June 4, 2020, 9:14pm UTC](https://discourse.julialang.org/t/ode-no-method-matching-similar/40764/3 "2020-06-04T21:14:10Z")

</div>

Thanks for the insight. That doesn’t work.  
I solved the problem by adding a second variable that does not evolve during the time … but that is creepy:

```julia
du[1] = pp*u[1]
du[2] = 0 

```

if you have better ideas! 🙂

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 4, 2020, 9:15pm UTC](https://discourse.julialang.org/t/ode-no-method-matching-similar/40764/4 "2020-06-04T21:15:09Z")

</div>

Was your initial condition a scalar? If it’s a scalar, why are you using the in-place form?

---

<div class="post-metadata">

### Author: ![99dB](https://avatars.discourse-cdn.com/v4/letter/9/838e76/32.png) [@99dB](https://discourse.julialang.org/u/99dB)
#### Post date: [June 4, 2020, 9:42pm UTC](https://discourse.julialang.org/t/ode-no-method-matching-similar/40764/5 "2020-06-04T21:42:25Z")

</div>

Yes it is a scalar. I am new to Julia so I don’t really understand what is the in-place form?

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [June 4, 2020, 9:46pm UTC](https://discourse.julialang.org/t/ode-no-method-matching-similar/40764/6 "2020-06-04T21:46:38Z")

</div>

`(du,u,p,t)` is in-place, modifying `du`. If you want to use scalars, use out-of-place:

```julia
function dudt(u,p,t)
  if rem(t,10) < 5
    pp = p[1]*t;
  end
  return pp*u
end

```

(the function doesn’t make sense of course because `pp` isn’t always defined). This is explained in the tutorial: [Ordinary Differential Equations · DifferentialEquations.jl](https://diffeq.sciml.ai/latest/tutorials/ode_example/)

---

<div class="post-metadata">

### Author: ![99dB](https://avatars.discourse-cdn.com/v4/letter/9/838e76/32.png) [@99dB](https://discourse.julialang.org/u/99dB)
#### Post date: [June 4, 2020, 9:57pm UTC](https://discourse.julialang.org/t/ode-no-method-matching-similar/40764/7 "2020-06-04T21:57:29Z")

</div>

Many thanks @ChrisRackauckas! It is working super well now. I understand the in-place now.
