# How to solve a system of nonlinear equations with a lagged variable?

**URL:** <https://discourse.julialang.org/t/how-to-solve-a-system-of-nonlinear-equations-with-a-lagged-variable/79949>\
**Category:** General Usage\
**Created:** [April 24, 2022, 6:13pm UTC](https://discourse.julialang.org/t/how-to-solve-a-system-of-nonlinear-equations-with-a-lagged-variable/79949 "2022-04-24T18:13:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fms-1988](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fms-1988/32/20275_2.png) [@fms-1988](https://discourse.julialang.org/u/fms-1988)\
**Post date:** [April 24, 2022, 6:13pm UTC](https://discourse.julialang.org/t/how-to-solve-a-system-of-nonlinear-equations-with-a-lagged-variable/79949/1 "2022-04-24T18:13:22Z")

</div>

I have a system of equations (f).  
This system has a set of variables, among them the variables (x\_t) and (x\_{t-1}).  
How can I represent the variable (x\_{t-1}) within this system?

Thanks in advance

---

<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:** [April 24, 2022, 6:34pm UTC](https://discourse.julialang.org/t/how-to-solve-a-system-of-nonlinear-equations-with-a-lagged-variable/79949/2 "2022-04-24T18:34:01Z")

</div>

> [@fms-1988](#):
>
> I have a system of equations (f).  
> This system has a set of variables, among them the variables (x\_t xtx\_t ) and (x\_{t-1} xt−1x\_{t-1} ).  
> How can I represent the variable (x\_{t-1} xt−1x\_{t-1} ) within this system?

You mean a discrete dynamical system?

[https://diffeq.sciml.ai/stable/types/discrete\_types/](https://diffeq.sciml.ai/stable/types/discrete_types/)

---

<div class="post-metadata">

**Author:** ![fms-1988](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fms-1988/32/20275_2.png) [@fms-1988](https://discourse.julialang.org/u/fms-1988)\
**Post date:** [April 25, 2022, 10:40am UTC](https://discourse.julialang.org/t/how-to-solve-a-system-of-nonlinear-equations-with-a-lagged-variable/79949/3 "2022-04-25T10:40:42Z")

</div>

Yes.  
Using “DifferentialEquations.jl” I understood how to specify a discrete problem (“give no dt and not tstops”).  
But I still don’t know how to represent a lagged variable.  
Here is an example for illustrative purposes only:

x\_{t} = a\cdot y\_{t-1}+ b  
y\_{t} = c\cdot x\_{t-1} + d\cdot x\_{t} + e   
z\_{t} = log(x\_{t}- y\_{t})

João Marcelo develops what I need, but using the R language. He identifies the lagged variables with [-1]

> **[GitHub - joaomacalos/sfcr: Simulate Stock-Flow Consistent Models](https://github.com/joaomacalos/sfcr)**
>
> Simulate Stock-Flow Consistent Models. Contribute to joaomacalos/sfcr development by creating an account on GitHub.

---

<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:** [April 25, 2022, 12:55pm UTC](https://discourse.julialang.org/t/how-to-solve-a-system-of-nonlinear-equations-with-a-lagged-variable/79949/4 "2022-04-25T12:55:48Z")

</div>

Oh I see, yeah you’ll need to put NonlinearSolve.jl in the right hand side.

---

<div class="post-metadata">

**Author:** ![fms-1988](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fms-1988/32/20275_2.png) [@fms-1988](https://discourse.julialang.org/u/fms-1988)\
**Post date:** [April 25, 2022, 1:13pm UTC](https://discourse.julialang.org/t/how-to-solve-a-system-of-nonlinear-equations-with-a-lagged-variable/79949/5 "2022-04-25T13:13:25Z")

</div>

That should work. I thought of creating a matrix with the results of NonlinearSolve.jl. Then I could call the elements of the last row of this matrix to be the lagged variables. Among the initial conditions would be these lagged variables.
