Variables & Functions
Variables & Functions
Out[]=
Building up multistep chemistry calculations is easier with the use of variables and functions.
Variables must start with letters but can also contain numbers.
Since capital letters are used for built-in objects, it’s best to start with lowercase letters:
a1/2
Out[]=
a1
2
A space between two variables or numbers is interpreted as multiplication.
To avoid confusion, utilize * explicitly:
a*b+5*x*x
Out[]=
ab+5
2
x
SameQ[a*b+5*x*x,ab+5xx]
Out[]=
True
Use /. and to make substitutions in an expression.
The “rule” can be typed as -> :
x+2y/.x->,y->
Out[]=
2+
1+2x/.x->2
Out[]=
5
Variables can be used in different ways in expressions.
Assign values using the = symbol:
x=2
Out[]=
2
Use your variable in other expressions:
1+2*x
Out[]=
5
Clear the assignment for x to remain unevaluated:
Clear[x]1+2*x
Out[]=
1+2x
Define your own functions with the construction f[x_]:=
In[]:=
f[x_]:=1+2*x
x_ means that x is a pattern that can have any value substituted for it.
:= means that any argument passed to f is substituted into the right-hand side upon evaluation:
:= means that any argument passed to f is substituted into the right-hand side upon evaluation:
f[2]
Out[]=
5
Out[]=