# Error Messages/41148

## Error Text

`A dynamic-loop dependency has been encountered at X[Time = 1] during the evaluation of Y[Time - «k»]. In a dynamic-loop cyclic dependency, a slice along the Time index depends on itself. To avoid this, you should ensure that this X depends on itself only at earlier Time points.`

## Cause

The Dynamic function allows you to create models where a variable ultimately depends on itself. However, it must still be the case that no variable value at a particular time point depends on itself at that same time point. For example, if `X[Time=1]` directly or indirectly depends on `X[Time = 1]`, then a cyclic dependency exists, and this error will result.

Consider this example:

``` Constant TN := 4
SysVar Time := 1..TN
Variable X := Dynamic(Y[Time = TN], Y[Time - 1] + 1)
Variable Y := Dynamic(1, X[Time - 1]*2)
```

Let's trace through the dependencies, starting with `X[Time = 1]` which clearly depends on `Y[Time = 4]`:

`X[Time = 1] ← Y[Time = 4] ← X[Time = 3] ← Y[Time = 2] ← X[Time = 1]`

As an exercise, trace through the dependencies when `TN := 5` (or any odd number ≥ 3) and show that in that case no such dynamic-cyclic dependency exists in the above example.

In the above example, we've jumped forward in time with the offset in the expression for `X[Time = 1]`. A more common mistake is to have a zero offset as you traverse through the loop, e.g.:

`Variable A := Dynamic(A0, F(B))`
`Variable B := Dynamic(B0, F(A))`

## Remedy

It is unusual to have forward and backward offsets in the same dynamic loop. Unless you are intentionally doing something unusual in that regard, make sure you have only backward offsets, or only forward offsets, in the dynamic loop. (If you have only forward offsets, to future time points, as may occur in dynamic programming, you're dynamic calls should also have the optional «reverse» parameter set to true for better efficiency). Then make sure you have at least one non-zero offset in any loop.

## Finding The Loop

In very large models, locating the loop can be difficult. To help with locating the loop, it is useful to make an evaluation trace, which will then often reveal the loop. To do this, press F12 to enter typescript, then type:

`verbosity : 134 { In Analytica 4.2 or before, use Verbosity: 6 }`
`photo C:\Temp\Trace.Log { Substitute your own log file name }`

Next, evaluate the node where the error occurs from. Then finally, back in typescript:

`endphoto`
`verbosity : 2 { remember to turn tracing back off }`

You can then use a text editor to view the log file. With `verbosity:134` indentation indicates the evaluation depth, which reveals where each variable evaluation was called from.

The verbosity system variable contains a bit field that controls various functions. The `verbosity: 2` bit should always be set (it controls whether certain error messages are reported when parsing definitions). The `verbosity: 4` bit causes each variable evaluation to be printed out, and the `verbosity: 128` bit controls whether these evaluations are indented to show evaluation depth (the 128 bit was added in Analytica 4.3). The other bits are not relevant here.