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

**URL:** <https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248>\
**Category:** Numerics\
**Created:** [October 4, 2022, 6:43pm UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248 "2022-10-04T18:43:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [October 4, 2022, 6:43pm UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/1 "2022-10-04T18:43:12Z")

</div>

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 ([https://www.youtube.com/watch?v=13hqE\_1a158](https://www.youtube.com/watch?v=13hqE_1a158)).

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.  
Thanks in advance for your help.  
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 - a_x^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”)

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [October 5, 2022, 11:40pm UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/2 "2022-10-05T23:40:08Z")

</div>

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 = 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”)

---

<div class="post-metadata">

**Author:** ![Funkerfish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/funkerfish/32/37982_2.png) [@Funkerfish](https://discourse.julialang.org/u/Funkerfish)\
**Post date:** [November 15, 2022, 9:04am UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/3 "2022-11-15T09:04:57Z")

</div>

try callback  
[https://diffeq.sciml.ai/stable/features/callback\_functions/#Using-Callbacks](https://diffeq.sciml.ai/stable/features/callback_functions/#Using-Callbacks)

[https://juliadynamics.github.io/DynamicalSystems.jl/latest/ds/integrators/#Using-callbacks-with-integrators](https://juliadynamics.github.io/DynamicalSystems.jl/latest/ds/integrators/#Using-callbacks-with-integrators)

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [November 18, 2022, 7:18pm UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/4 "2022-11-18T19:18:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [November 18, 2022, 7:48pm UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/6 "2022-11-18T19:48:50Z")

</div>

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

```julia
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") 

```

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [November 19, 2022, 5:57am UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/7 "2022-11-19T05:57:16Z")

</div>

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:

```julia
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[5] += 90.5
cb = DiscreteCallback(condition,affect!)
tr = trajectory(ds, 500, callback=cb,tstops=[400])

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:

![DiscreteSyst_discourse_callback2](https://global.discourse-cdn.com/julialang/original/3X/7/a/7af8a40d010a57188c90f58fd54e3d576bfc33e9.png)

As may be observed in the figure, there is no bump or discontinuity in period 400.

---

<div class="post-metadata">

**Author:** ![Funkerfish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/funkerfish/32/37982_2.png) [@Funkerfish](https://discourse.julialang.org/u/Funkerfish)\
**Post date:** [November 19, 2022, 8:22am UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/8 "2022-11-19T08:22:04Z")

</div>

```julia
using DynamicalSystems,DifferentialEquations

using Plots

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

x, y = u

a, b, c, d, E = p

du[1] = a*x + b*y

du[2] = 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[5] += 90.5

cb = DiscreteCallback(condition,affect!)

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

sol = solve(prob,callback=cb)

plot(sol)

```

---

<div class="post-metadata">

**Author:** ![Funkerfish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/funkerfish/32/37982_2.png) [@Funkerfish](https://discourse.julialang.org/u/Funkerfish)\
**Post date:** [November 19, 2022, 8:28am UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/9 "2022-11-19T08:28:03Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [November 19, 2022, 7:39pm UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/10 "2022-11-19T19:39:44Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![josec](https://avatars.discourse-cdn.com/v4/letter/j/ec9cab/32.png) [@josec](https://discourse.julialang.org/u/josec)\
**Post date:** [November 19, 2022, 9:56pm UTC](https://discourse.julialang.org/t/dynamicalsystems-jl-how-to-change-the-value-of-a-parameter-after-a-certain-period/88248/11 "2022-11-19T21:56:26Z")

</div>

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