Disclaimer: I am new to Mathematica but have some experience with other programming languages.
I have defined the following function (simplified version):
Module[{x, pTwo, y, FOC, Profits, solution, p}, F1[] := {
x := ( v 2 \[Beta] + 2 \[\Beta] vTwo - 2 p) / (2 t - 2 \[Beta] t);
pTwo := (v - x (t - \[Theta])) / 2;
y := (v - pTwo) / (t - \[Theta]);
Profits[p_] := p x + \[Beta] pTwo (y - x);
FOC = D[Profits[p], p] ;
solution = Solve[FOC == 0, p];
p = Simplify[ p /. solution[[1]] ];
x = Simplify [x[p]];
pTwo = Simplify[pTwo[p]];
y = Simplify[y[p]];
p, x, pTwo, y}]
When I run the function first time it works fine, but on the second run, it crashes. The third run works again, and so on. It crashes with the following message:
"D: [Long Expression Omitted] is not a valid variable"
Notably, the expression it returns does not contain p - perhaps implying that the code is substituting some value for p.
To try to resolve the error, I have done the following
Define x, pTwo, y as functions of p. This didn't help. The error message was different though
"SetDelayed: Tag times in [Long Expression Omitted]... [p_] is Protected"
Use ClearSystemCache["Symbolic"] at end of the function. No help
Disable Dynamic Updating. No help
NB: I need to use the Module because I have several other functions where I use the same variables. This is not the source of the error. Even when I just run and test one of the functions (without defining the others), I get this weird oscillating behavior between running and crashing.
At this point I am completely lost. What causes this esoteric error? I suspect it is something to do with scope or use of := but I cannot figure it out.
F1[]:=
inside the module? The syntax for making function isF[x_,y_]:=Module[{local variables}, body]
Then you call it asF[3,5]
as example. $\endgroup$