Also, use a double equal for equations. A single equal means `Set[]`, which is used to define the left-hand-side. A typical use of `=` is give parameters value, as you do near the beginning of your code. A requirement for the left hand side is that it has be a symbol or an expression with a head that is symbol; in each case, the symbol must not be `Protected` or `Locked`. When one mistakenly uses `=` instead of `==`, then the left-hand side sometimes is an expression whose head is `Protected`. For instance, the head of `2 x` is `Times`, which can be seen in either of the following:
Head[2x]
Out[]=
Times
FullForm[2x]
Out[]//FullForm=
Times[2,x]
This leads to an error in the first code below:
In[]:=
2x=3
Set
:Tag Times in 2x is Protected.
Out[]=
3
2x==3(*acorrectlyspecifiedequation*)
If you're using Mathematica, there is a red, three-dot button. In the pop-up menu, you can click on the documentation link, https://reference.wolfram.com/language/ref/message/General/write.html, where this is explained.
A single equal in place of a double equal also leads to the recursion limit error:
Clear[x,y]​​eq1=x=y-y^2​​eq2=y=x+x^2
Out[]=
y-
2
y
$RecursionLimit
:Recursion depth of 1024 exceeded.
Out[]=
TerminatedEvaluation[RecursionLimit]
The correct way to define equations:
Clear[x,y]​​eq1=x==y-y^2​​eq2=y==x+x^2
Out[]=
xy-
2
y
Out[]=
yx+
2
x
Solve[{eq1,eq2},{x,y}]
Out[]=
{{x-2,y2},{x0,y0}}
To illustrate Gianluca's point (an underscore is the character for `Blank[]`):
In[]:=
x_2=3
Set
:Tag Times in 2x_ is Protected.
Out[]=
3
FullForm[x_2]
Out[]//FullForm=
Times[2,Pattern[x,Blank[]]]