0
$\begingroup$

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

  1. 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"

  2. Use ClearSystemCache["Symbolic"] at end of the function. No help

  3. 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.

$\endgroup$
4
  • 1
    $\begingroup$ When I run the function could you show how you called it? Why do you have F1[]:= inside the module? The syntax for making function is F[x_,y_]:=Module[{local variables}, body] Then you call it as F[3,5] as example. $\endgroup$
    – Nasser
    Commented Jun 7 at 11:28
  • 1
    $\begingroup$ btw, there are many other problems in your code. You have many symbols used inside the module which are global. This is not good. Anything used inside module should be either passed in as parameter or be local to the module. Avoid using global symbols/variable inside a module. $\endgroup$
    – Nasser
    Commented Jun 7 at 11:47
  • $\begingroup$ @Nasser Thank you for your comment. The problem was how I had defined the function. Defining it as ´F[x_,y_]:=Module[{local variables}, body]´ solved the problem. Do you want to post your comment as an answer so that I can accept it? $\endgroup$
    – Leksa99
    Commented Jun 7 at 13:55
  • $\begingroup$ I am glad you were able to solve it. It is ok,. you can either post and accept your own answer if you want or close your question. it is up to you. $\endgroup$
    – Nasser
    Commented Jun 7 at 21:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.