I was wondering how to approximate or tabulate values for this numeric approximation: It is the following: The confusing part is how to implement the subscripts in mathematica.

$y_{i+1} = (t_i - y_i)h + y_i$

I want to do something of the following for this numeric solution.

x[t_] = t/(1 + t^4);
data = N[Table[{t, x[t]}, {t, -5, 5}], 2];
tableofcontents = Prepend[data, {"t", "x(t)"}];
Text@Grid[tableofcontents, Alignment -> Right, Dividers -> {Center, {False, True}}, 
  Spacings -> 1]

Mathematica graphics


I recommend you look at RecurrenceTable as J. M. suggested. However to give you an idea of how you would implement subscripts (by which I think you mean recursion) in a "normal" way, here's a simple example using the Fibonacci sequence:

f[0] = 1;
f[1] = 1;
f[n_] := f[n - 1] + f[n - 2];

For performance you would also "memoize", which just means that you store every result so that you don't have to recalculate it later:

f[0] = 1;
f[1] = 1;
f[n_] := (
   f[n] = f[n - 1] + f[n - 2]

Notice that the f[n] = within the function is not f[n_] :=, because we aren't matching to a generic pattern and we don't need to recalculate the expression every time it's used. We're telling Mathematica that f applied to the specific value that n has at that time is whatever it calculates on the right-hand side at that time, so the usage there is like array indexing. It's the same as was done in the first two lines specifying f[0] and f[1].

And for the sake of completeness, here's the RecurrenceTable version of the Fibonacci sequence that I pulled straight out of the documentation:

RecurrenceTable[{a[n] == a[n - 1] + a[n - 2], a[1] == 1, a[2] == 1}, a, {n, 10}]
| improve this answer | |

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.