2
$\begingroup$

I have a discrete dynamic model with two variables $x$ and $y$ as:

$x_t =ax_{t-1}+by_{t-1}$

$y_t =cx_{t-1}+dy_{t-1}$

where $a, b, c, d$ are parameters.

I would like to plot how the movement of x[t] changes when a model parameter changes using DiscretePlot. Is there any way I can see the result in one diagram. For instance, I would like to see the movement of $x_t$ when $a=0.1$ and that when $a=0.2$ in one diagram so that the difference can be more explicit. I would like to do the same for $y_t$ as well.

My code so far is:

x[t_] := x[t] = a*x[t - 1] + b*y[t - 1]
y[t_] := y[t] = c*x[t - 1] + c*y[t - 1]

with initial values as:

x[0] = 1;
y[0] = 1;

and parameter values as:

a = 0.1
b = 0.2
c = 0.3
d = 0.4

My code for plot is simply this:

Plot1 = Show[GraphicsRow[{DiscretePlot[{x[t]}, {t, 0, 10}, PlotLabel -> "x", 
BaseStyle -> {FontSize -> 10}, Filling -> None, Joined -> True], 
DiscretePlot[{y[t]}, {t, 0, 10}, PlotLabel -> "y", 
BaseStyle -> {FontSize -> 10}, Filling -> None, 
Joined -> True]}], ImageSize -> Full]
Export["Plot1.pdf", Plot1]
$\endgroup$
1
  • 2
    $\begingroup$ It might be more expedient for you to reformulate this as repeated multiplication of a matrix with a vector: MatrixPower[{{a, b}, {c, d}}, t, {1, 1}]. $\endgroup$ Commented Feb 21, 2016 at 2:39

1 Answer 1

5
$\begingroup$
Clear[a, b, c, x, y, sol, sol2]

Use RSolve for recursive equations.

Using the equations in the code blocks,

sol[a_, b_, c_] =
  RSolve[{x[t] == a*x[t - 1] + b*y[t - 1], 
     y[t] == c*x[t - 1] + c*y[t - 1],
     x[0] == 1, y[0] == 1}, {x[t], y[t]}, t][[1]];

Manipulate[
 DiscretePlot[
  Evaluate[{x[t], y[t]} /. sol[a, b, c]],
  {t, 0, 10},
  AxesLabel -> {"t", None},
  BaseStyle -> {FontSize -> 10},
  Filling -> None,
  Joined -> True,
  PlotLegends -> {"x[t]", "y[t]"}],
 {{a, .1}, .1, .5, .025, Appearance -> "Labeled"},
 {{b, .2}, .125, .5, .025, Appearance -> "Labeled"},
 {{c, .3}, .1, .5, .025, Appearance -> "Labeled"}]

enter image description here

Using the original equations

sol2[a_, b_, c_, d_] =
  RSolve[{x[t] == a*x[t - 1] + b*y[t - 1], y[t] == c*x[t - 1] + d*y[t - 1],
     x[0] == 1, y[0] == 1}, {x[t], y[t]}, t][[1]];

Manipulate[
 DiscretePlot[
  Evaluate[{x[t], y[t]} /. sol2[a, b, c, d]],
  {t, 0, 10},
  AxesLabel -> {"t", None},
  BaseStyle -> {FontSize -> 10},
  Filling -> None,
  Joined -> True,
  PlotLegends -> {"x[t]", "y[t]"}],
 {{a, .1}, .1, .5, .025, Appearance -> "Labeled"},
 {{b, .2}, .125, .5, .025, Appearance -> "Labeled"},
 {{c, .3}, .1, .5, .025, Appearance -> "Labeled"},
 {{d, .4}, .1, .5, .025, Appearance -> "Labeled"}]

enter image description here

EDIT: Looking only at x[t] for various values of a

DiscretePlot[
 Evaluate[Table[x[t] /. sol2[a, .2, .3, .4], {a, {.1, .2}}]],
 {t, 0, 10},
 AxesLabel -> {"t", "x[t]"},
 BaseStyle -> {FontSize -> 10},
 Filling -> None,
 Joined -> True,
 PlotLegends -> {"a = 0.1", "a = 0.2"}]

enter image description here

$\endgroup$
3
  • $\begingroup$ thanks so much! In fact, what I would like to see is two different evolutions of x[t] appearing in one diagram; one for, say, a being 0.1 and the other for a being 0.2. Let us forget about the other variable, y[t]. Can I do this without using Manipulate? $\endgroup$
    – ppp
    Commented Feb 21, 2016 at 3:29
  • $\begingroup$ @ppp - see last edit. $\endgroup$
    – Bob Hanlon
    Commented Feb 21, 2016 at 3:37
  • $\begingroup$ Great! Thanks so much! $\endgroup$
    – ppp
    Commented Feb 21, 2016 at 3:46

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.