# Julia code for 1-D discrete system

**URL:** <https://discourse.julialang.org/t/julia-code-for-1-d-discrete-system/54403>\
**Category:** Modelling & Simulations\
**Created:** [February 1, 2021, 6:20pm UTC](https://discourse.julialang.org/t/julia-code-for-1-d-discrete-system/54403 "2021-02-01T18:20:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mukherji](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mukherji](https://discourse.julialang.org/u/mukherji)\
**Post date:** [February 1, 2021, 6:20pm UTC](https://discourse.julialang.org/t/julia-code-for-1-d-discrete-system/54403/1 "2021-02-01T18:20:38Z")

</div>

> I have a relatively straightforward one-dimensional discrete system that is a variation of the logistic equation. I cannot figure out how to create my own function in Julia. For example, how do I model x(t+1) = p[1]+p[2]\*x(t)^0.8(1-p[3]\*x(t)^0.2). I cannot figure out how to create something like this. I can run the logistic equation successfully. The other examples I see are mainly with models with 2 or more variables. I cannot figure out how to set up the system for a customized 1-D problem. Thanks.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [February 1, 2021, 7:21pm UTC](https://discourse.julialang.org/t/julia-code-for-1-d-discrete-system/54403/2 "2021-02-01T19:21:41Z")

</div>

Hi, welcome to the Julia discourse

> [@mukherji](#):
>
> I cannot figure out how to create something like this. I can run the logistic equation successfully.

What precisely did you try? Can you show the code?

What I believe the answer is:

```julia
using DynamicalSystems

f(x, p, t) = p[1]+p[2] * x^0.8 * (1-p[3]*x^0.2)
x0 = 0.1 # initial condition
p0 = [0.1, 0.1, 0.1] # parameters
ds = DiscreteDynamicalSystem(f, x0, p0)
tr = trajectory(ds, 100) # timeseries

```

```julia
julia> tr = trajectory(ds, 100)
101-element Array{Float64,1}:
 0.1
 0.11484893192461114
 0.11655674050586243
 0.11674997309813506
 0.11677179773573809
 0.11677426222298418
 0.11677454051213455
 0.11677457193638044
 0.11677457548478662
 0.11677457588547042
 0.11677457593071539
 0.11677457593582442
 0.11677457593640134
 ⋮
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476
 0.11677457593647476

```

---

<div class="post-metadata">

**Author:** ![mukherji](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mukherji](https://discourse.julialang.org/u/mukherji)\
**Post date:** [February 1, 2021, 7:56pm UTC](https://discourse.julialang.org/t/julia-code-for-1-d-discrete-system/54403/3 "2021-02-01T19:56:24Z")

</div>

Thank you very much!  
I was trying variations of the following:

using DynamicalSystems  
using PyPlot  
logistic\_eom(x, p, t) = SVector{1}(p[1]\*x1)  
state = zeros(1)  
p = [2.0]  
logistic = DiscreteDynamicalSystem(logistic\_eom, state, p)

This yields:  
DimensionMismatch(“derivative(f, x) expects that x is a real number. Perhaps you meant gradient(f, x)?”)

Stacktrace:  
[1] derivative(::Function, ::StaticArrays.SArray{Tuple{1},Float64,1,1}) at C:\Users\mukherji.julia\packages\ForwardDiff\kU1ce\src\derivative.jl:72  
and similar errors.

---

<div class="post-metadata">

**Author:** ![apo383](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/apo383/32/11272_2.png) [@apo383](https://discourse.julialang.org/u/apo383)\
**Post date:** [February 1, 2021, 10:58pm UTC](https://discourse.julialang.org/t/julia-code-for-1-d-discrete-system/54403/4 "2021-02-01T22:58:33Z")

</div>

The problem is that you are mixing scalar and array representations of state, plus `x1` is not defined:

```julia
logistic_eom(x, p, t) = SVector{1}(p[1]*x1)
state = zeros(1)

```

Note that `x1` is a scalar that doesn’t appear in the argument list, and `state` is a 1-D vector. You should follow @Datseris’s answer very closely, where `x` is a scalar and `f` returns a scalar.  
According to the [documentation](https://juliadynamics.github.io/DynamicalSystems.jl/dev/ds/general/):

> **Comment on 1-D**  
> One dimensional discrete systems expect the state always as a pure number, `0.8` instead of `SVector(0.8)` .

You’ll probably want `logistic_eom(x1,p,t) = p[1]*x1` to be consistent on both left and right-hand sides, and your initial state will also have to be scalar.

EDIT: Forgot to say try to format your code for readability and follow [these instructions](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757).

---

<div class="post-metadata">

**Author:** ![mukherji](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@mukherji](https://discourse.julialang.org/u/mukherji)\
**Post date:** [February 2, 2021, 3:36am UTC](https://discourse.julialang.org/t/julia-code-for-1-d-discrete-system/54403/5 "2021-02-02T03:36:24Z")

</div>

Thank you for your comments! Datseris’s response indeed solves my problem.
