LESSON 5
Mathematica  Solves Equations (including Differential Equations)

Author's notice

This tutorial was written by 

Fred Goodman
Department of Mathematics
University of Iowa
Iowa City, IA 52242
goodman@math.uiowa.edu

This material may be freely copied and used for non-commercial purposes.  Please retain this author's notice, and do not distribute any altered version of this material without the author's permission  (unless  the material is altered beyond recognition).

Preliminary:  Transformation rules

A transformation rule is a rule such as
x->3
for substituting a value for a variable.
You can apply such a rule to an expression by writing
(any old expression)/.x->3
This will replace every occurance of x in the expression by 3. 

The following cell is an input cell; to do the indicated computation, click on the red bracket to the right, and hit "Enter" or "Shift+Return".  Do the same with subsequent input cells.

x^2  +  3  x  - 5  /. 
      x  ->  3

x^2  +  3  x  - 5  /. x  ->  a

Mathematica  can solve equations.

Mathematica can solve ordinary algebraic equations, using the command Solve.  


Notice the use of the double equal sign in the following command.  An equation is always written with ==; if you use single equal sign, you will get some nonsense response.

Solve[(x^2) + 18 x - 5 == 0, x]

The solution to the quadratic equation is given in terms of transformation rules.  To recover a list of numerical solutions,   you can apply the transformation rules to the expression x:

soln = Solve[(x^2) + 18 x - 5 == 0, x]
x /. soln

To get numerical solutions of algebraic equations, you can use NSolve instead of Solve.

NSolve[(x^7) - 3 (x^4) + 25 (x^2) - 5 == 0, x]

Solve and NSolve cannot solve equations involving transcendental functions such as Exp and Sin.  For example:

NSolve[(x^2) - Cos[x] == 0, x]

To solve such an equation, you need to use the command FindRoot, which requires you to give an initial guess for the solution.   Graphing the function f[x] = (x^2) - Cos[x] shows there ought to be two solutions (which are opposites of one another).

Plot[(x^2) - Cos[x], {x, -5, 5}]

So we can take 2 for an initial guess to find a solution:

FindRoot[(x^2) - Cos[x] == 0, {x, 2}]

Mathematica  can solve differential equations.

Mathematica can solve ordinary differential equations, using the command DSolve.    DSolve attempts to find an exact symbolic solution to the differential equation when possible.

DSolve[{y'[x]==3 y[x] + (x^2),y[0] == 0},y[x],x]

The solution to the differential equation is given as a transformation rule.  Actually, the solution is given as a list of transformation rules, with only one element in the list

To recover a solution as a function which can be evaluated or plotted, you have to apply the transformation rule.

soln=DSolve[{y'[x]==3 y[x] + (x^2),y[0] == 0},y[x],x]
soln[[1]]
y[x] /. soln[[1]]

f[x_]  :=  
      y[x]  /. soln[[1]]

    Plot[f[x],  {x, -4,  3}]

The command NDSolve is used to compute numerical solutions of Initial Value Problems

NDSolve[{y'[x]==y[x] (1 - y[x]^2),y[0] == (1/2)},y,{x, -10, 10}]

The answer is given as a transformation rule for the variable y.  The expression on the right hand side of the transformation rule just announces that numerical data has been tabulated.  To recover the solution as a function which can be evaluated and plotted, you apply the transformation rule (just as with DSolve):

soln = NDSolve[{y'[x]==y[x] (1 - y[x]^2),y[0] == (1/2)},y,{x, -10, 10}]
f[
        x_]  :=  y[x] /.  soln[[1]]
Plot[
      f[x],  {x,  -10,  10}]