2
$\begingroup$

The following pair of coupled recursion equations represents movement between population 1 (x[1, t]) and population 2 (x[2, t]) over time (t).

x[1, t_] := x[1, t] = (1 - m) x[1, t - 1] + m x[2, t - 1]
x[2, t_] := x[2, t] = (1 - m) x[2, t - 1] + m x[1, t - 1]

x[1, 0] := 1
x[2, 0] := 0

I want to write the coupled recursion equations as one line instead of two (similar to below) where the first argument describes the population number. However, in the body of the equation, I don't know how to tell Mathematica that if k = 1, l should equal 2, and if k = 2, l should equal 1.

x[k_,t_] := x[k,t] = (1-m) x[k, t - 1] + m x[l, t - 1]

x[1, 0] := 1
x[2, 0] := 0

Is this possible in the context of a recursion equation? Maybe there is a way to define an argument that can only take on two values?

$\endgroup$
4
  • 2
    $\begingroup$ Something like x[k_, t_] := x[k, t] = (1 - m) x[k, t - 1] + m x[3 - k, t - 1] or x[k_, t_] := x[k, t] = (1 - m) x[k, t - 1] + m x[If[k == 2, 1, 2], t - 1]. $\endgroup$
    – march
    Commented Sep 16, 2016 at 23:02
  • $\begingroup$ The latter is clever and appears to work! It does add a little to the evaluation time but maybe that can't be helped. $\endgroup$ Commented Sep 16, 2016 at 23:12
  • $\begingroup$ Doesn't the first one work? I would expect it to be faster. $\endgroup$
    – march
    Commented Sep 16, 2016 at 23:14
  • $\begingroup$ Oh wait! That is faster! I was worried Mathematica would think it should iterate over k also if there was k - stuff but stuff - k seems to work. $\endgroup$ Commented Sep 16, 2016 at 23:26

1 Answer 1

1
$\begingroup$

A simpler way to write this is

M = {{1 - m, m}, {m, 1 - m}};
X[t_] := X[t] = M.X[t - 1]
X[0] := {1, 0}

Alternatively, you can recognise this as a matrix power, applied to a constant vector

X1[t_] := MatrixPower[M, t, {1, 0}]

In this case, it can be evaluated symbolically

X1[t]
(* {1/2 + 1/2 (1 - 2 m)^t, 1/2 - 1/2 (1 - 2 m)^t} *)
$\endgroup$
1
  • $\begingroup$ An added advantage of this approach is seeing your model as a matrix population model, for which much theory has been developed (e.g. Caswell 2000). $\endgroup$
    – Chris K
    Commented Sep 17, 2016 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.