I am very new to Mathematica and StackExchange, so pardon me if I am repeating a question that has already been answered. I am trying to use Mathematica to do the following: u[n+1] = A.u[n] where A is a square tridiagonal matrix and u[0] is a column vector. I want to be able to run this for any number of iterations, as many as I need. I then want to do a 3d plot of the results. My skills are very rudimentary. I would include what I already tried, but I havent been able to do any of it yet.

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 0) Browse the common pitfalls question 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Dr. belisarius Feb 17 '16 at 23:57
  • 1
    $\begingroup$ For starters NestList[A.#&, u[0],5] $\endgroup$ – Dr. belisarius Feb 17 '16 at 23:58
  • 1
    $\begingroup$ To keep the syntax close to what you have written: u[n_] := u[n] = a.u[n - 1]; Then specify a and u[1] and ask for u[10] or u[100]. $\endgroup$ – bill s Feb 18 '16 at 0:02
  • 2
    $\begingroup$ Presumably, since A is a fixed matrix, you'll want to use the action form of MatrixPower[]: MatrixPower[A, 5, u[0]]. $\endgroup$ – J. M.'s torpor Feb 18 '16 at 1:00
  • 1
    $\begingroup$ No, I am using the explicit finite difference method to solve the heat equation. I am comparing that to solving the heat equation using Fourier Series. $\endgroup$ – arrowdog Feb 18 '16 at 4:24

As @Dr. belisarius stated, NestList[A.#&, u[0],5] would work. (# and & are pure functions. This guide may help)

If you want to keep the values of u[1], u[2], etc.:

u[n_] := u[n] = A . u[n - 1];
u[0] = {x, y, z}; (* your definition here *)

The u[n] = part after u[n_] := is optional (memoization), but it is a good practice to have it if you have a recursion formula. It saves memory by preventing redundant computations.

Then for a 3D plot: (replace 10 with whatever number of point you want)

ListPointPlot3D[Table[u[n], {n, 0, 10}]


Edit: A useful function to use could be NestList. It performs a given operation a given number of times and outputs all of the steps. For example, NestList[f, x, 3] gives {x, f[x], f[f[x]], f[f[f[x]]]}.

Use this instead if you do not want to keep the values of u[n]:

 NestList[Function[vector, A . vector], u[0], 10]

You could replace Function[...] with its pure-function counterpart, A.#&.

  • $\begingroup$ I would really like the 3d plot to be a contour plot that shows each iteration, u[1], u[2], u[3], ... progressing through time, not stacked on top of one another. Not sure how to do this in this instance. $\endgroup$ – arrowdog Feb 18 '16 at 4:30
a = RotationMatrix[Pi/20, {0, 1, 1}] // N;
start = {1, 0, 0};
Graphics3D[{Blue, Arrowheads[.03], Arrow@Partition[#, 2, 1], Red, 
    PointSize[Medium], Point@#, Green, PointSize[.03], 
    Point@start}] &@NestList[a.# &, start, 40]

Mathematica graphics


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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