0
$\begingroup$

I am trying to iterate a function over 1000 time units using the NestWhile function and Table functions in Mathematica. I have defined all my variables and my function as follows:

  μ = 200
    δ = 0.2
    σ = 0.105
    γ = 0.6



 f[t_, α_] := 
     f[t, α] = 
      f[t - 1, α] + α (σ μ + γ ((σ \
    μ)/δ - g[t - 1, α]) + 
      ArcTan[f[t - 1, α] - μ] - σ f[t - 1, α])
    f[0, α_] = 10

    g[t_, α_] := 
     g[t, α] = (1 - δ) g[
     t - 1, α] + σ*μ + γ (σ (μ/\
    δ) - g[t - 1, α]) + ArcTan[f[t - 1, α] - μ]
    g[0, α_] = 20

I then employed

w[t_, α_] := 
 w[t, α] = 
  NestWhile[f[t, α], 0, Abs[#] < 1000. &, 1, 100]

result = Table[{α, w[t, α]}, {t, 1, 1000}, {α, 2, 
   15, 0.02}]

and the output iterated the function perfectly, except with every single coordinate gave a [0] at the end, for instance

{{2., 148.769[0]}, {2.02, 150.157[0]}, {2.04, 151.544[0]}, {2.06, 
  152.932[0]}, {2.08, 154.32[0]}, {2.1, 155.707[0]}, {2.12, 
  157.095[0]}, {2.14, 158.483[0]}, {2.16, 159.87[0]}, <<633>>, {14.84,
   1039.67[0]}, {14.86, 1041.05[0]}, {14.88, 1042.44[0]}, {14.9, 
  1043.83[0]}, {14.92, 1045.22[0]}, {14.94, 1046.6[0]}, {14.96, 
  1047.99[0]}, {14.98, 1049.38[0]}, {15., 1050.77[0]}}.

Can someone please help? I would really appreciate it! Thank you. My goal is to plot the results on a diagram of /alpha vs. w[t,/alpha], but this is stopping me! Thanks again!

$\endgroup$

1 Answer 1

1
$\begingroup$

Forget about w. You simply don't need it. You can generate the table directly just by evaluating f.

μ = 200;
δ = 0.2;
σ = 0.105;
γ = 0.6;

f[0, α_] = 10;
f[t_, α_] := 
  f[t, α] = 
    f[t - 1, α] + α (σ μ + γ ((σ μ)/δ - g[t - 1, α]) + 
      ArcTan[f[t - 1, α] - μ] - σ f[t - 1, α])

g[0, α_] = 20;
g[t_, α_] := 
  g[t, α] = 
    (1 - δ) g[t - 1, α] + σ μ + γ (σ (μ/δ) - g[t - 1, α]) + 
      ArcTan[f[t - 1, α] - μ]

DownValues[f] = Take[DownValues[f], -2];
DownValues[g] = Take[DownValues[g], -2];
Table[{α, f[t, α]}, {α, 2, 3}, {t, 8}]
{{{2, 148.769}, {2, 178.704}, {2, 186.445}, {2, 189.39}, 
  {2, 191.079}, {2, 192.289}, {2, 193.227}, {2, 193.971}}, 
 {{3, 218.153}, {3, 250.4}, {3, 243.132}, {3, 232.188}, 
  {3, 223.482}, {3, 217.261}, {3, 212.93}, {3, 209.928}}}
$\endgroup$
1
  • $\begingroup$ @woreo. I'm glad you find my answer useful. Please consider accepting it. You can do that by clicking on the check mark that appears on the left of the answer below the down arrow. $\endgroup$
    – m_goldberg
    Jun 10, 2017 at 23:33

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.