next up previous
Next: Differential Equations Up: Computer Algebra Previous: Basic Principles

Solving Equations

Before considering how to solve simple equations using Mathematica we have to note one other detail of notation, $==$ is the symbol for Boolean equality. So, for example


In[25]:= x=4
Out[25]= $\displaystyle{4}$
In[26]:= x==4
Out[26]= $\displaystyle{\mbox{True}}$
In[27]:= x==5
Out[27]= $\displaystyle{\mbox{False}}$
In[28]:= x
Out[28]= $\displaystyle{4}$


from which we see that the result of x==y is either "True" or "False". Contrast this with the symbol $=$ which is used to assign a value to a symbol.

Now we introduce the Solve function for solving equations. the syntax can be seen from the following examples:


In[29]:= Solve[4x + 8 == 0, x]
Out[29]= $\displaystyle{\{\{x\mathrel{-\negthinspace\negthinspace>}-2\}\} }$
In[30]:= Solve[x^2 + 2x - 7 == 0, x]
Out[30]= $\displaystyle{\{\{x \mathrel{-\negthinspace\negthinspace>}{-2-4\mathop{\rm Sqrt...
...{-\negthinspace\negthinspace>}{-2+4\mathop{\rm Sqrt}\nolimits [2]\over 2}\}\} }$
In[31]:= N[%]
Out[31]= $\displaystyle{\{x \mathrel{-\negthinspace\negthinspace>}-3.82843, x \mathrel{-\negthinspace\negthinspace>}1.82843\}\} }$
In[32]:= Solve[a x^2 + b x + c == 0, x]
Out[32]= $\displaystyle{\{\{ x \mathrel{-\negthinspace\negthinspace>}{-{b} + {\mathop{\rm...
...egthinspace>}{-{b} - {\mathop{\rm Sqrt}\nolimits [b^2 - 4 a c]}\over 2 a}\}\} }$


where both numerical and symbolic solutions may be obtained. We can also find more complicated problems, such as


In[33]:= Solve[{x^2 + y^2 == 1, x + 3 y == 0}, {x, y}]
Out[33]= $\displaystyle{\{\{x\mathrel{-\negthinspace\negthinspace>}{-3\over\mathop{\rm Sq...
...l{-\negthinspace\negthinspace>}-({1\over\mathop{\rm Sqrt}\nolimits [10]})\}\} }$
In[34]:= x/.%
Out[34]= $\displaystyle{\{x\mathrel{-\negthinspace\negthinspace>}{-3\over\mathop{\rm Sqrt...
...athrel{-\negthinspace\negthinspace>}{3\over\mathop{\rm Sqrt}\nolimits [10]}\} }$
In[35]:= y/.%%
Out[35]= $\displaystyle{\{y\mathrel{-\negthinspace\negthinspace>}{1\over\mathop{\rm Sqrt}...
...rel{-\negthinspace\negthinspace>}-({1\over\mathop{\rm Sqrt}\nolimits [10]})\} }$


in which we should note the syntax for solving systems of equations by grouping things in braces, $\{\}$, and for extracting parts of the solution. Note also how the different, distinct solutions are grouped together.

Unfortunately there are equations which Mathematica cannot solve so simply, but which with a little help it could do so. One very useful command in this context is Eliminate, used as follows


In[36]:= Eliminate[{a x + y == 0, 2 x + (1 - a) y == 1}, y]
Out[36]= $\displaystyle{- (a x) + a^2 x == 1 - 2 x}$
In[37]:= Solve[%, x]
Out[37]= $\displaystyle{\{\{x\mathrel{-\negthinspace\negthinspace>}{1\over 2 - a + a^2}\}\}}$


Often Mathematica is unable to solve a problem in a single step but can do so if the user guides it through the steps. This is particularly the case when an equation may have unphysical solutions which can be ignored. The user can recognise the physically important solutions and concentrate on those.


next up previous
Next: Differential Equations Up: Computer Algebra Previous: Basic Principles