# DynamicalSystems.jl: how to change the value of a parameter after a certain period

Hi. I hope this question is placed in the proper category.
I have studied the DynamicalSystems.jl guidelines also followed in a very useful video by Datseris (Intro to dynamical systems in Julia - YouTube).

Suppose I have the code below, which is a slightly modified version of the first system that appears in the Datseris’s guide. My question is: how can I change a parameter like A (in the system below) after period 20 for example. That is, in the code below, how can I have parameter A equal to 1 in periods 1 to 19, and then, starting in period 20, the parameter A becomes 1.02.
I have tried to do that with various forms of “for” loops but my code has not worked.
This is the code:

using DynamicalSystems
using Plots
function henon_rule(u, p, n)
x, y = u # system state
A, a, b = p # system parameters
xn = A - ax^2 + y
yn = b
x
return SVector(xn, yn)
end

p = [1.0, 1.4, 0.3]
u0 = [0.2, 0.3]

ds = DiscreteDynamicalSystem(henon_rule, u0, p)
tr = trajectory(ds, 100) # trajectory for 100 steps

## The Plots

plot(tr[:,1], label=“x”, xlabel=“t”, ylabel=“Value of x, y”, title=“Datseris’s DynamicalSystems Intro”)
plot!(tr[:,2], label=“y”, xlabel=“time”)

May be my post was not very clear. I will try to explain a bit more.
What I would like to do is increase the value of a parameter starting in some period. The idea is to find the number of periods that it takes the system to reach a new steady state after the shock (I mean, after the increase in the parameter).
Below is the code I am using on a system, with a “for” loop.
The problem is that the increase in the parameter “E” is not shown in the plot. Any help will be appreciated.

using DynamicalSystems
using Plots

function test(u, p, n)
x, y = u
a, b, c, d, E = p
xn = ax + by
yn = cx + dy + E
return SVector(xn, yn)

``````for n=[400:500]
E=2.5
end
``````

end

p = [1.0, 0.1, -0.01, 0.98, 0.025]
u0 = [0.0, 0.0]

ds = DiscreteDynamicalSystem(test, u0, p)
tr = trajectory(ds, 500)

plot(tr[:,1], label=“x”, xlabel=“t”, ylabel=“Value of x, y”, title=“Test”)
plot!(tr[:,2], label=“y”, xlabel=“time”)

Thank you very much for the suggestion.
From what I could see, the “callbacks” may be used in DifferentialEquations.jl or in DynamicalSystems.jl, but only for continuous systems. The system I am trying to simulate is discrete (what I mean is that it is a system of difference equations).
Thanks again.

I just realized that the code was incorrectly formatted. I should have used backticks.
This is the way it the code should look:

``````using DynamicalSystems
using Plots

function test(u, p, n)
x, y = u
a, b, c, d, E = p
xn = a*x + b*y
yn = c*x + d*y + E
return SVector(xn, yn)

for n=[400:500]
E=2.5
end
end

p = [1.0, 0.1, -0.01, 0.98, 0.025]
u0 = [0.0, 0.0]

ds = DiscreteDynamicalSystem(test, u0, p)
tr = trajectory(ds, 500)

plot(tr[:,1], label="x", xlabel="t", ylabel="Value of x, y", title="Test")
plot!(tr[:,2], label="y", xlabel="time")
``````

I ran the following code, based on the suggestion to use the “callback” option. The code ran just fine, but the discontinuity does not show in the data or the plot. This is the code:

``````using DynamicalSystems
using Plots

function test(u, p, n)
x, y = u
a, b, c, d, E = p
xn = a*x + b*y
yn = c*x + d*y + E
return SVector(xn, yn)
end

p = [1.0, 0.1, -0.01, 0.98, 2.5]
u0 = [0.0, 0.0]

ds = DiscreteDynamicalSystem(test, u0, p)
tr = trajectory(ds, 500)

condition(u,n,integrator) = n==400
affect!(integrator) = integrator.p += 90.5
cb = DiscreteCallback(condition,affect!)
tr = trajectory(ds, 500, callback=cb,tstops=)

plot(tr[:,1], label="x", xlabel="t", ylabel="Value of x, y", title="Test")
plot!(tr[:,2], label="y", xlabel="time")
``````

The code above generates this plot: As may be observed in the figure, there is no bump or discontinuity in period 400.

``````using DynamicalSystems,DifferentialEquations

using Plots

function test!(du,u, p, n)

x, y = u

a, b, c, d, E = p

du = a*x + b*y

du = c*x + d*y + E

end

p = [1.0, 0.1, -0.01, 0.98, 2.5]

u0 = [0.0, 0.0]

condition(u,n,integrator) = n==400

affect!(integrator) = integrator.p += 90.5

cb = DiscreteCallback(condition,affect!)

prob = DiscreteProblem(test!,u0,(0,500),p)

sol = solve(prob,callback=cb)

plot(sol)
``````

Sorry I don’t know how to use callback in `ds = DiscreteDynamicalSystem`. From the tutorial I can only know that it works on the step!(integrator)

Oh thank you very much.
Your suggestion was very useful, and I will keep trying to find out how to properly use the callbacks.
Thanks again!!!

This code is extremely useful: the plot you got is exactly the one I should get.
Thank you so very much for your help!!!