7
$\begingroup$

How do I write a recursive equation to compute a list of answers? I tried NestList, but it didn't work.

A = {{.5, -.6}, {.75, 1.1}};
x0 = {2, 0};

Dot[A,x0]
(* {1., 1.5} *)

Dot[A, {1.`, 1.5`}]
(* {-0.4, 2.4} *)

Dot[A, Dot[A, {1.`, 1.5`}]]
(* {-1.64, 2.34} *)
$\endgroup$

3 Answers 3

13
$\begingroup$

You were correct. NestList is exactly the function you want to use.

NestList[Dot[A, #]&, x0, 5]

(* {{2, 0}, {1., 1.5}, {-0.4, 2.4}, {-1.64, 2.34}, {-2.224, 
   1.344}, {-1.9184, -0.1896}} *)

Note that the first argument of NestList must be a function.

$\endgroup$
3
  • $\begingroup$ @ JHM foolish me that I didn't realize x0 is the one being operated on, thanks. $\endgroup$
    – DSL
    Commented Oct 27, 2016 at 3:40
  • $\begingroup$ How to visualize the change in x0,x1,x2.. with the no. of operations? $\endgroup$
    – thils
    Commented Oct 27, 2016 at 4:49
  • $\begingroup$ @thils ListPlot would work. $\endgroup$ Commented Oct 27, 2016 at 5:26
10
$\begingroup$

Your can use MatrixPower for this example:

f[n_] := MatrixPower[{{.5, -.6}, {.75, 1.1}}, n].{2, 0}
f /@ Range[0, 5]

yields:

{{2., 0.}, {1., 1.5}, {-0.4, 2.4}, {-1.64, 2.34}, {-2.224, 
  1.344}, {-1.9184, -0.1896}}
$\endgroup$
2
  • $\begingroup$ Or #.x0 & /@ NestList[Dot[A, #] &, A, 5] to make it a recursion (starts at matrix power 1). $\endgroup$ Commented Nov 2, 2016 at 9:14
  • 2
    $\begingroup$ @JacobAkkerboom thank you. Yes, the best recursive answer was already provided. I was just providing another in-built approach. :) $\endgroup$
    – ubpdqn
    Commented Nov 2, 2016 at 9:17
2
$\begingroup$

You can also do this recursively, very nearly as you wrote it:

a[k_] := a[k] = A.a[k - 1];
a[1] = x0;

Now you can calculate any desired iterate by asking for a[5] or a[10]. Or calculate a range of values:

a[#] & /@ Range[5]
{{2, 0}, {1., 1.5}, {-0.4, 2.4}, {-1.64, 2.34}, {-2.224, 1.344}}
$\endgroup$

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.