One major activity at most any level of mathematics is solving equations, a
task at which Mathematica excels.
For example, let's solve a simple equation in one unknown:
Solve[2*x + 5 == 0, x]
Notice that Solve[ , ] takes two arguments: the equation to be solved and
the unknown to be solved for. Since none of the numbers in the equation
are floating point numbers, Mathematica
prints out the solution as an exact rational number. There are two ways to get
the answer into floating point form. You can write at least one of the numbers
in floating point form, or you can use NSolve instead of Solve:
NSolve[2*x + 5 == 0, x]
Whereas Solve tries to solve equations symbolically, NSolve tries
to solve them numerically (there are very many other Mathematica functions which follow this same pattern, such as Integrate and NIntegrate for integrating functions analytically or
numerically). The distinction may be vague to you at the moment,
but it should be clarified in future lessons.
You can save away the result of solving an equation, just as you can save the
result of anything else, by using assignment as you learned in the last lesson:
result = Solve[2*x + 5==0,x]
The careless use of assignment can cause problems when you are doing symbolic
computations, and now is a good time to point that out. Consider, for
example, the following computation:
Solve[result + 5 ==0, result]
5 5
Solve[{{5 + (x -> -(-))}} == 0, {{x -> -(-)}}]
2 2
Can you explain what happens?
Even though result looks like a variable in the call to solve, it really
isn't because we've previously given it a value. So what you're really asking
Mathematica to solve is the equation
-5/2 + 5 = 0
for the ``variable'' -5/2, which of course doesn't make any sense.
This is one of the most confusing aspects of Mathematica. The best policy is to use
different symbols for the variables to which you assign values than you do for
the variables that are true mathematical unknowns.
It is possible to ``unassign'' a variable that contains a value:
result=.
Having ``unassigned'' result, we can now solve the equation that
couldn't be solved before:
Solve[result + 5 == 0, result]