# Periodic solution to a forced oscillator ODE

**URL:** <https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080>\
**Category:** Modelling & Simulations\
**Tags:** ode, mechanics\
**Created:** [November 26, 2024, 8:40am UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080 "2024-11-26T08:40:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [November 26, 2024, 8:40am UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/1 "2024-11-26T08:40:48Z")

</div>

I’m working with an ODE model. I’m looking for the periodic solution to a forced, non-conservative, non-linear and time-invariant oscillator.

For example, something like `x''(t) = - k (x(t) + 0.1 x^3(t)) - a x'(t) + A sin(w t)`

I was able to work with my model using `DifferentialEquations: ODEProblem, solve`. I can add a generic forced input function, simulate it and even fit the model using least-squares, what is super awesome. How can I enforce the periodic solution constraint, though? Are there any special methods I should be looking into, and are they available in Julia?

Caveats:

- I’m assuming a drag term and forced input are sufficient conditions to one periodic solution of the same frequency. Perhaps more solutions are possible with strong non-linearities, in which case I’m happy obtaining just something close to an initial guess. I’m expecting I can set the model parameters, including the amplitude and frequency, and I’ll obtain different waveshapes for the solution as I play around with the values.
- I’d love to find closed formula solutions but I wouldn’t be surprised to learn that’s impossible, and I’m happy obtaining just numerical solutions.
- I have a feeling there must be some major theory I’m ignoring about solving this kind of problem and I’d love to hear any insights.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [November 26, 2024, 9:36am UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/2 "2024-11-26T09:36:00Z")

</div>

For me, I would use spectral expansion in the time direction, and then it’s turned into a set of algebraic equations.

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [November 26, 2024, 10:14am UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/3 "2024-11-26T10:14:17Z")

</div>

Do you mean I should just represent the solution as vectors (I actually have two variables), and treat it as a Taylor or Fourier series? Sounds good to me, how would I proceed to compute a solution then?

My non-linearities can be a little more complicated that just x^3, how can that interefere with this approach? And I guess my main question might be: can I actually approach this with some custom method, just solving a big non-linear system for instance, and not really relying on DifferentialEquations.jl at all?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [November 26, 2024, 12:53pm UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/4 "2024-11-26T12:53:18Z")

</div>

> [@xor0110](#):
>
> How can I enforce the periodic solution constraint, though?

BVProblem, since that’s a boundary value problem.

> [@photor](#):
>
> For me, I would use spectral expansion in the time direction, and then it’s turned into a set of algebraic equations.

That can work in this specific case but it’s not generally stable.

> [@xor0110](#):
>
> can I actually approach this with some custom method, just solving a big non-linear system for instance, and not really relying on [DifferentialEquations.jl](https://juliahub.com/ui/Packages/General/DifferentialEquations) at all?

That’s what a BVP solver will do, the MIRK methods in BoundaryValueDiffEq, plus a bit of adaptivity.

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [November 26, 2024, 2:31pm UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/5 "2024-11-26T14:31:13Z")

</div>

Of course Fourier expansion, which means your periodic constraints are automatically satisfied. I usually use Mathematica’s FindRoot to do that, no need to use DifferentialEquations.jl

---

<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:** [November 26, 2024, 3:09pm UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/6 "2024-11-26T15:09:47Z")

</div>

BifurcationKit is precisely done for this. Check [🟠 Periodic predator-prey model · Bifurcation Analysis in Julia](https://bifurcationkit.github.io/BifurcationKitDocs.jl/dev/tutorials/ode/tutorialsCodim2PO/#Periodic-predator-prey-model)

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [November 26, 2024, 8:33pm UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/7 "2024-11-26T20:33:36Z")

</div>

All right, I totally overlooked the BVP section. It seems to work fine. Thank you!

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [November 26, 2024, 8:34pm UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/8 "2024-11-26T20:34:55Z")

</div>

Cool, I’ll take a look at it. This forced example seems to be exactly what I needed. Am I correct to say it is required for the equations not to be dependent on time, that’s why you need these u and v variables to produce the sinusoid?

---

<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:** [November 27, 2024, 6:42am UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/9 "2024-11-27T06:42:54Z")

</div>

You are correct. This will be changed in the near future

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [November 27, 2024, 11:30am UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/10 "2024-11-27T11:30:23Z")

</div>

Does [GitHub - NonlinearOscillations/HarmonicBalance.jl: A Julia package for solving nonlinear differential equations using the harmonic balance method.](https://github.com/NonlinearOscillations/HarmonicBalance.jl) help here?

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [November 27, 2024, 5:32pm UTC](https://discourse.julialang.org/t/periodic-solution-to-a-forced-oscillator-ode/123080/11 "2024-11-27T17:32:22Z")

</div>

This looks very interesting, definitely will study in more detail, thanks!
