1

Is this the right way to use for loop in order to find the sequence of $x_n$

x[0] = 0;
T[x_] := Piecewise[{{1 - x, 0 <= x < 1/7}, {(x + 6)/7, 
     1/7 <= x <= 1}}];
a[n_] := n/(n + 1);
b[n_] := n/(n + 5);
x[n_] := (1 - a[n - 1]) x[n - 1] + 
   a[n - 1]*T[(1 - b[n - 1]) x[n - 1] + b[n - 1] T[x[n - 1]]];
For[n = 1, n < 20, n++, Print[x[n]]];
RunnyKine
  • 33,088
  • 3
  • 109
  • 176

1 Answers1

2
Clear[x];

x[0] = 0;

T[x_] := Piecewise[{{1 - x, 0 <= x < 1/7}, {(x + 6)/7, 1/7 <= x <= 1}}];

a[n_] := n/(n + 1);

b[n_] := n/(n + 5);

Include memorization (Functions That Remember Values They Have Found) in the definition of x for effciency

x[n_] := x[n] = (1 - a[n - 1]) x[n - 1] + 
    a[n - 1]*T[(1 - b[n - 1]) x[n - 1] + b[n - 1] T[x[n - 1]]];

A For loop is inefficient. Use Map (/@)

N[x /@ Range[20]]

(*  {0., 0.440476, 0.773254, 0.926828, 0.980189, 0.99535, 0.999033, 0.999819, 
0.999969, 0.999995, 0.999999, 1., 1., 1., 1., 1., 1., 1., 1., 1.}  *)
Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198