Message Boards Message Boards

Recursion depth of 1024 exceeded error

Posted 1 month ago

Here's my code and I keep getting the error. Don't know how to work around it. I am new to mathematica and coding in general and don't know how to fix this -

(* Define the exogenous value for k *)
k = 0.7;  (* Replace with your desired k *)

expectedValues[m_] := Module[{cache = {}},
  If[MemberQ[cache, m],
    Return[cache[[Position[cache, m][[1, 1]]]]] 
  ];

  {Eaa, Eab, Eba, Ebb} = {m*NIntegrate[a, {b, 0, m}, {a, m, 1 + m}] / NIntegrate[1, {b, 0, m}, {a, m, 1 + m}] + (1 - m)*NIntegrate[a, {b, m,1}, {a, b, 1 + m}] / NIntegrate[1, {b, m, 1}, {a, b, 1 + m}],
    (1 - m) * NIntegrate[b, {a, m, 1}, {b, 0, a}] / NIntegrate[1, {a, m, 1}, {b, 0, a}] +  (m)*NIntegrate[b, {a, m, 1 + m}, {b, 0, 1}] / NIntegrate[1, {a, m, 1 + m}, {b, 0, 1}],
    NIntegrate[a, {b, m, 1}, {a, m, b}] / NIntegrate[1, {b, m, 1}, {a, m, b}],
    NIntegrate[b, {a, m, 1}, {b, a, 1}] / NIntegrate[1, {a, m, 1}, {b, a, 1}]};

  cache = Join[cache, {{m, {Eaa, Eab, Eba, Ebb}}}];
  {Eaa, Eab, Eba, Ebb}
];

drawIndifferenceLine[m_, k_] := 
 Module[{a, b, points, aRange, expectedVals},
  aRange = {m, 1 + m};
  points = {};

  expectedVals = Evaluate[expectedValues[m]];

  Do[
    b = Solve[
      Sqrt[k]*expectedVals[[1]]*a + Sqrt[1 - k]*expectedVals[[2]]*b == 
       Sqrt[k]*expectedVals[[4]]*b + Sqrt[1 - k]*expectedVals[[3]]*a, 
      b][[1, 1]];

    If[Im[b] == 0 && 0 < b < 1,
      points = Append[points, {a, b}]
    ];
    , {a, aRange[[1]], aRange[[2]], 0.01}
   ];

  points
  ];

allData = {};
Do[
  allData = Append[allData, {m, drawIndifferenceLine[m, k]}];
, {m, 0.3, 1, 0.05}];

ListPlot[Map[{#1[[1]], #2[[1]]} &, allData], PlotRange -> {{0, 2}, {0, 1}}, AspectRatio -> 1]
POSTED BY: Anne Shirley
2 Replies

Also, when you write

expectedValues[m_] := Module[{cache = {}},

the variable cache is local. At the end of every call of expectedValues, the content of cache will be thrown away, I think.

POSTED BY: Gianluca Gorni

When you call b = Solve[...] you get

b = b -> 1.0915057091078622`  a

and that generates infinite recursion.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract