# BifurcationKit: record\_from\_solution Periodic Orbits

**URL:** https://discourse.julialang.org/t/bifurcationkit-record-from-solution-periodic-orbits/123814
**Category:** Modelling & Simulations
**Created:** [December 13, 2024, 3:51pm UTC](https://discourse.julialang.org/t/bifurcationkit-record-from-solution-periodic-orbits/123814 "2024-12-13T15:51:48Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mmplac14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmplac14/32/213427_2.png) [@mmplac14](https://discourse.julialang.org/u/mmplac14)
#### Post date: [December 13, 2024, 3:51pm UTC](https://discourse.julialang.org/t/bifurcationkit-record-from-solution-periodic-orbits/123814/1 "2024-12-13T15:51:48Z")

</div>

Hi!

I am using BifurcationKit to compute the periodic orbits of the Jansen-Rit model from the first Hopf Point. I seem to be having issues computing them for y1 - y2. I am using record\_from\_solution to compute them for y1-y2 because it seems to me that if I don’t use record\_from\_solution only the values for y0 are contained in the result.

I am getting this (and the branch should not be flicking back up)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/2/c2d3461c11e4bbfa5433203c47f7344656c14946.png)

It should stop at p =113, as shown here:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/b/bbb4d9490e35a0e924cba01ee619c73e0885b5a5.png)

Here is my full code:

```julia
using Revise, Plots
using BifurcationKit

const BK = BifurcationKit

# Define the function σ
function σ(v, e0 = 2.5, v0 = 6, r=0.56)
    2 * e0 / (1 + exp(r * (v0 - v)))
end

# vector field
function jansenrit(z, param)
	(;A, a, B, b, C1, C2, C3, C4, p) = param #These are the parameters. p will be our control paramter
	y0, y3, y1, y4, y2, y5 = z #These are our functions
	[
        #We define the derivative functions in the same order as above
	    y3
        A * a * σ(y1 - y2) - 2 * a * y3 - a^2 * y0
        y4
        A * a * (p + C2 * σ(C1 * y0)) - 2 * a * y4 - a^2 * y1
        y5
        B * b * C4 * σ(C3 * y0) - 2 * b * y5 - b^2 * y2
	]
end

# parameter values
C= 135
par = (A = 3.25, a = 100, B = 22, b = 50, C1 = C, C2 = 0.8 * C, C3 = 0.25 * C, C4 = 0.25 * C, p = 50.0)

# initial condition
z0 = [0.0, 0.0, 15.0, 0.0, 10.0, 0.0]

# prob = BifurcationProblem(jansenrit, z0, par, (@optic _.p);
# record_from_solution = (x, p; k...) -> (y0 = x[1], y3 = x[2], y1 = x[3], y4 = x[4], y2 = x[5], y5 = x[6], y = x[3]-x[5]),)

prob = BifurcationProblem(jansenrit, z0, par, (@optic _.p);
	record_from_solution = (x, p; k...) -> (y = x[3] - x[5]),)

opts_br_po = ContinuationPar(p_min =-50.0, p_max = 400.0, max_steps = 3000, dsmin = 1e-7, ds = 1e-3, newton_options = NewtonPar(tol = 1e-10, verbose = true), detect_bifurcation = 3, tol_stability = 1e-4)

br_po = @time continuation(br, 4, opts_br_po,
                    PeriodicOrbitOCollProblem(50,5; jacobian = BK.DenseAnalytical(), meshadapt = true);
                    verbosity = 0,
                    alg = PALC(tangent = Bordered()),
                    linear_algo = COPBLS(),
                    normC = norminf,
                    callback_newton = BK.cbMaxNorm(1e2), #limit residual to avoid Inf or NaN
                    record_from_solution = (x, p; k...) -> (y = x[3] - x[5])
)

#Plotting
# Colour by stability of each periodic orbit bracn
stability_po = [stable ? :green : :red for stable in br_po.stable]

# Plot the equilibria branch
plot(br, branchlabel = "equilibria")
# Plot the max and min of the periodic orbit branch
plot!(br_po, c = colour_stability)

```

After some experimenting, I believe this is due to “record\_from\_solution”. If I set record\_from\_solution to y0, but don’t specify it in the Periodic Orbit Continuation, I get the result I expect for y0. However, if I use “record\_from\_solution” to record y0, I also get strange behaviour, with the branch flipping back up and joining with the bifurcation point.

**— Without setting record\_from\_solution** :

```julia
br_po = @time continuation(br, 4, opts_br_po,
                    PeriodicOrbitOCollProblem(50,5; jacobian = BK.DenseAnalytical(), meshadapt = true);
                    verbosity = 0,
                    alg = PALC(tangent = Bordered()),
                    linear_algo = COPBLS(),
                    normC = norminf,
                    callback_newton = BK.cbMaxNorm(1e2), #limit residual to avoid Inf or NaN
)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/e/2e7f09e32b24da1e1c34df598307ff2d626573a4.png)

**—With record\_from\_solution set to y0:**

```julia
br_po = @time continuation(br, 4, opts_br_po,
                    PeriodicOrbitOCollProblem(50,5; jacobian = BK.DenseAnalytical(), meshadapt = true);
                    verbosity = 0,
                    alg = PALC(tangent = Bordered()),
                    linear_algo = COPBLS(),
                    normC = norminf,
                    callback_newton = BK.cbMaxNorm(1e2), #limit residual to avoid Inf or NaN
                    record_from_solution = (x, p; k...) -> (y = x[1])
)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/3/734d300022bcc7c579c482de5969a8fa32b52e56.png)

Lastly, it seems like I can’t get both the **min and max values of the periodic orbit** with the way I set y0 or y1-y2 in “record\_from\_solution”. Is there a way to do so?

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [December 13, 2024, 5:52pm UTC](https://discourse.julialang.org/t/bifurcationkit-record-from-solution-periodic-orbits/123814/2 "2024-12-13T17:52:35Z")

</div>

For periodic orbits, you need to specify a different record function to reproduce the figure. Please have a look [here](https://bifurcationkit.github.io/BifurcationKitDocs.jl/dev/tutorials/ode/tutorialsODE/#Branch-of-periodic-orbits-with-Orthogonal-Collocation) for example. Basically, by default, it returns the period. If you plot `x[1]`, it has a difficult meaning because it is involved in the variables describing the periodic orbit.

---

<div class="post-metadata">

### Author: ![mmplac14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmplac14/32/213427_2.png) [@mmplac14](https://discourse.julialang.org/u/mmplac14)
#### Post date: [December 16, 2024, 9:09am UTC](https://discourse.julialang.org/t/bifurcationkit-record-from-solution-periodic-orbits/123814/3 "2024-12-16T09:09:30Z")

</div>

Thanks! I have used this and the shape is correct now:

```julia
args_po = (	record_from_solution = (x, p; k...) -> begin
		xtt = get_periodic_orbit(p.prob, x, p.p)
		return (max = maximum(xtt[1,:]),
				min = minimum(xtt[1,:]),
				period = getperiod(p.prob, x, p.p))
	end,
	plot_solution = (x, p; k...) -> begin
		xtt = get_periodic_orbit(p.prob, x, p.p)
		arg = (marker = :d, markersize = 1)
		plot!(xtt.t, xtt[1,:]; label = "y0", arg..., k...)
		end,
	# we use the supremum norm
	normC = norminf)

```

However, it seems like the stability has been affected (red = unstable, green= stable)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/5/75b323fc4a95200826c9e98f5a472213e9af1f30.png)

whereas without the args\_po or record\_from\_solution it is like this (with the same continuation parameters)

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/8437739505d4d67c67306e82bb61b3d1c7bc6b23.png)

---

<div class="post-metadata">

### Author: ![RayleighLord](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rayleighlord/32/33592_2.png) [@RayleighLord](https://discourse.julialang.org/u/RayleighLord)
#### Post date: [December 16, 2024, 9:29am UTC](https://discourse.julialang.org/t/bifurcationkit-record-from-solution-periodic-orbits/123814/4 "2024-12-16T09:29:04Z")

</div>

The stability is highlighted using the thickness of the line, not with the color, right?

---

<div class="post-metadata">

### Author: ![mmplac14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mmplac14/32/213427_2.png) [@mmplac14](https://discourse.julialang.org/u/mmplac14)
#### Post date: [December 16, 2024, 9:54am UTC](https://discourse.julialang.org/t/bifurcationkit-record-from-solution-periodic-orbits/123814/5 "2024-12-16T09:54:24Z")

</div>

False alarm. I had a typo in my code. Thanks everyone!

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [December 16, 2024, 10:34am UTC](https://discourse.julialang.org/t/bifurcationkit-record-from-solution-periodic-orbits/123814/6 "2024-12-16T10:34:09Z")

</div>

exactly!

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [December 16, 2024, 10:35am UTC](https://discourse.julialang.org/t/bifurcationkit-record-from-solution-periodic-orbits/123814/7 "2024-12-16T10:35:40Z")

</div>

glad it worked. Do not hesitate to post issues or things you found difficult to use
