# DifferentialEquations simple usage not working

**URL:** <https://discourse.julialang.org/t/differentialequations-simple-usage-not-working/69985>\
**Category:** General Usage\
**Tags:** diffeq, ode\
**Created:** [October 18, 2021, 4:23pm UTC](https://discourse.julialang.org/t/differentialequations-simple-usage-not-working/69985 "2021-10-18T16:23:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [October 18, 2021, 4:23pm UTC](https://discourse.julialang.org/t/differentialequations-simple-usage-not-working/69985/1 "2021-10-18T16:23:25Z")

</div>

Hi there,  
I have a very simple ODE problem defined by:

```julia
f_norm(x, α) = x - α*(1 - exp(-x))
f_norm_prime(x, α) = 1 - α*exp(-x)
f_norm_dprime(x, α) = α*exp(-x)
function dxdN!(dx, x, α, N)
    f = f_norm(x, α)
    fx = f_norm_prime(x, α)
    fxx = f_norm_dprime(x, α)
    dx = 3*(2*f - x*fx)/(x*fxx - fx)
end
using DifferentialEquations
prob = ODEProblem(dxdN!, x0, Nspan, α)

```

Up until here everything seems to work fine and the ODE initial value problem seems to be accepted and well understood by the DifferentialEquations.jl package. However, when I run:

```julia
sol =solve(prob)

```

I get the following error:

```julia
ERROR: MethodError: no method matching similar(::Float64, ::Type{Float64})
Closest candidates are:
  similar(::Union{LinearAlgebra.Adjoint{T, var"#s832"}, LinearAlgebra.Transpose{T, var"#s832"}} where {T, var"#s832"<:(AbstractVector{T} where T)}, ::Type{T}) where T at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/adjtrans.jl:230
  similar(::Union{LinearAlgebra.Adjoint{T, S}, LinearAlgebra.Transpose{T, S}} where {T, S}, ::Type{T}) where T at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/adjtrans.jl:234
  similar(::Union{LinearAlgebra.Adjoint{T, S}, LinearAlgebra.Transpose{T, S}} where {T, S}, ::Type{T}, ::Tuple{Vararg{Int64, N}}) where {T, N} at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/adjtrans.jl:235

```

What happens? I guess it must be something very silly of me and I thank you in advance.

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [October 18, 2021, 5:11pm UTC](https://discourse.julialang.org/t/differentialequations-simple-usage-not-working/69985/2 "2021-10-18T17:11:06Z")

</div>

Hi, the following works:

```julia
using DifferentialEquations
f_norm(x, α) = x - α*(1 - exp(-x))
f_norm_prime(x, α) = 1 - α*exp(-x)
f_norm_dprime(x, α) = α*exp(-x)
function dxdN(x, α, N)
    f = f_norm(x, α)
    fx = f_norm_prime(x, α)
    fxx = f_norm_dprime(x, α)
    dx = 3*(2*f - x*fx)/(x*fxx - fx)
end
using DifferentialEquations

Nspan = (0.0,10.0)
α = 0.5
x0 = 1.0
prob = ODEProblem(dxdN, x0, Nspan, α)

sol = solve(prob)

```

There are two forms for an ODE function, dxdN(x,alpha,N) and dxdN!(dx,x,alpha,N). The second version is what is called mutating, meaning that you should alter the first argument and not have the function return anything like this:

```julia
function dxdN!(dx, x, α, N)
    f = f_norm(x, α)
    fx = f_norm_prime(x, α)
    fxx = f_norm_dprime(x, α)
    dx[1] = 3*(2*f - x*fx)/(x*fxx - fx)
    return nothing
end

```

However, for this version you have to alter your f\_norm etc to take vector arguments, or call each function like f\_norm( x[1] , alpha ).

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [October 18, 2021, 5:27pm UTC](https://discourse.julialang.org/t/differentialequations-simple-usage-not-working/69985/3 "2021-10-18T17:27:12Z")

</div>

Dear Jonas,

Could you give some more references (tutorials, manuals) about those differences between dxdN(x, alpha, N) and dxdN!(dx, x, alpha, N). It seems to me this is not a specific issue for DifferentialEquations.jl, but for mutating functions in general, right? Also could you dwell a little bit more on making sense of all this right from the error message, if possible at all?

Thanks a lot!

---

<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:** [October 19, 2021, 11:57am UTC](https://discourse.julialang.org/t/differentialequations-simple-usage-not-working/69985/4 "2021-10-19T11:57:08Z")

</div>

> [@mocalvao](#):
>
> Could you give some more references (tutorials, manuals) about those differences between dxdN(x, alpha, N) and dxdN!(dx, x, alpha, N).

[https://tutorials.sciml.ai/html/introduction/03-optimizing\_diffeq\_code.html](https://tutorials.sciml.ai/html/introduction/03-optimizing_diffeq_code.html) describes it in detail.

---

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [October 19, 2021, 3:55pm UTC](https://discourse.julialang.org/t/differentialequations-simple-usage-not-working/69985/5 "2021-10-19T15:55:28Z")

</div>

Thanks a lot Chris.
