# Differential Equations and Parameter Estimation

**URL:** <https://discourse.julialang.org/t/differential-equations-and-parameter-estimation/24246>\
**Category:** General Usage\
**Created:** [May 15, 2019, 4:52pm UTC](https://discourse.julialang.org/t/differential-equations-and-parameter-estimation/24246 "2019-05-15T16:52:09Z")\
**Posts on this page:** 2\
**Page:** 2

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [July 2, 2019, 5:41pm UTC](https://discourse.julialang.org/t/differential-equations-and-parameter-estimation/24246/22 "2019-07-02T17:41:52Z")

</div>

Chris: based on the idea of a student, I extended his idea to the following utility function for zero order spline _with constant step length_ in the abscissa:

```julia
function zero_spline(x,dx,y)
    if x < 0
        return y[1]
    elseif x >= length(y)*dx
        return y[end]
    else
        return y[trunc(Int,x/dx)+1]
    end
end

```

I’ve run some simple benchmarks based on a vector pair `xd`, `yd` each of `length(xd) = 10_001`:

```julia
julia> y = ZeroSpline(yd,xd)
julia> x = range(0,8000*60,length=10^4)
julia> @benchmark y.(x)
BenchmarkTools.Trial: 
  memory estimate: 78.27 KiB
  allocs estimate: 3
  --------------
  minimum time: 202.679 ms (0.00% GC)
  median time: 228.310 ms (0.00% GC)
  mean time: 225.889 ms (0.00% GC)
  maximum time: 245.988 ms (0.00% GC)
  --------------
  samples: 23
  evals/sample: 1

```

If I instead use my utility function, I find:

```julia
julia> y1 = (x) -> zero_spline(x,60,xd)
julia> @benchmark y1.(x)
BenchmarkTools.Trial: 
  memory estimate: 539.33 KiB
  allocs estimate: 29497
  --------------
  minimum time: 809.201 μs (0.00% GC)
  median time: 1.401 ms (0.00% GC)
  mean time: 1.498 ms (3.67% GC)
  maximum time: 6.355 ms (38.53% GC)
  --------------
  samples: 3327
  evals/sample: 1

```

OK… the `ZeroSpline` instantiator uses ca. 7 times less memory, but “my” utility function is 150 times faster. If I _remove_ the “safeguards” which allows for extrapolation, my code is a little bit faster, but not much.

**Questions**

1. Is there a version of `ZeroSpline` for the case of constant abscissa step size?
2. Would a `ZeroSpline` instantiator with constant abscissa step size be as fast as “my” utility function?
3. Is there a way to reduce the memory usage of my utility function?

Of course, I realize that the `DataInterpolations` package is much more fancy than my simple utility function.

---

<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:** [July 2, 2019, 7:20pm UTC](https://discourse.julialang.org/t/differential-equations-and-parameter-estimation/24246/23 "2019-07-02T19:20:09Z")

</div>

> [@BLI](#):
>
> - Is there a version of `ZeroSpline` for the case of constant abscissa step size?
> - Would a `ZeroSpline` instantiator with constant abscissa step size be as fast as “my” utility function?
> - Is there a way to reduce the memory usage of my utility function?

It would be nice to just add this in. I think what would do it is just a specialization of `findfirst` for when `t` is an `AbstractRange`. Just add a branch and ZeroSpline should get the speed increase you see from the utility function. Sounds like a good PR 🙂

[Previous page](https://discourse.julialang.org/t/differential-equations-and-parameter-estimation/24246.md?page=1)
