Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]
share|improve this question
2  
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}]. – J. M. Feb 21 at 2:39
up vote 5 down vote accepted
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

share|improve this answer
    
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? – ppp Feb 21 at 3:29
    
@ppp - see last edit. – Bob Hanlon Feb 21 at 3:37
    
Great! Thanks so much! – ppp Feb 21 at 3:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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