Julia code for 1-D discrete system

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.

Hi, welcome to the Julia discourse

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

What I believe the answer is:

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> 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
2 Likes

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.

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

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:

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.

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