Solving Equations

This file contains an outline of how to use the software package Mathematica to solve equations.


1. The Solve Command

When solving polynomial equations of degree 4, we can always find the exact algebraic solutions with the Solve command. These will include all complex solutions.

To solve for x in the equation f(x) = c, the general command is Solve[f(x)==c,x]


Example: Solve for x : 3 x^2 - 4 x - 6 = 0 .

To do so open a new Mathematica file, then type and enter the following command : (Note : A space represents multiplication.) You can also highlight and copy the line from this page and then paste it into an open Mathematica page.

Solve[3 x^2-4 x-6==0,x]

We obtain two real solutions. To see if they can be simplified further, enter the following :
(% always represents the previous output.)

Simplify[%]


Example : Solve for x : x^4 - 3 x^3 - 3 x^2 + 61 x = 156.

Solve[x^4-3 x^3-3 x^2+61 x==156,x]

We obtain two real solutions and two imaginary solutions.


We can also define a function f(x) before solving an equation.

Example : Let f(x) = 5 x - 8 x^(1/2) + 10. For which x does f(x) = 20?
For which x does f(x) = 2?

To solve the first equation, enter the following : (Note : A "*" can also be used for multiplication.)

Clear[f,x]

f[x_]=5*x-8*Sqrt[x]+10;

Solve[f[x]==20,x]

We obtain one real solution.


To solve f(x) = 2, enter Solve[f[x]==2,x]. Upon doing so, we see that there are two imaginary solutions to this equation, but no real solutions. We can verify this result by observing the graph of f. Since f is only defined for x >= 0, enter the following :

Plot[f[x],{x,0,10},AxesOrigin->{0,0}]

Observe that f does not cross the line y = 2.


Let's see if we can solve the general quadratic and cubic equations. You'll recognize the first solution and then you'll see why you never had to learn the "cubic formula". In case variables have been previously defined, we first Clear them.

Clear[a,b,c,x]

Solve[a*x^2+b*x+c==0,x]

Solve[a*x^3+b*x^2+c*x+d==0,x]


2. The NSolve command

Most times for polynomial equations of degree > 2, the Solve command will give us a very complicated algebraic expression. Therefore, it is sometimes better to use NSolve which gives the numerical form of the algebraic solution and which also works for polynomials of degree >= 5. Compare the following :

Solve[x^3-3 x^2-4 x==-9,x]

NSolve[x^3-3 x^2-4 x==-9,x]


3. The Plot/FindRoot Method

Most often there is no algebraic solution to an equation; therefore, we have no choice but to find approximate numerical solutions. For example, let's try to solve the equation .4 x^2 - 10 sinx = 2. First try Solve or NSolve :

NSolve[.4 x^2-10 Sin[x]==2,x]

Since there appears to be no algebraic solution, we must approximate. To numerically solve the equation f(x) = c, we first subtract c to set the equation to 0 : f(x) - c = 0. We then Plot the graph of y = f(x) - c to visually approximate the roots.

f[x_]= .4 x^2 - 10 Sin[x];

Plot[f[x]-2,{x,-50,50},AxesOrigin->{0,0}]

From the graph, we can see that there are several places where f(x) - 2 equals 0 (i.e. crosses the x-axis). We can now zoom in to get a better look :

Plot[f[x]-2,{x,-10,10},AxesOrigin->{0,0}]

We now see 4 roots : x ~ -5, x ~ -3.5, x ~ 0, x ~ 3. We now apply the FindRoot command 4 times with each of these values. If r is an initial visual approximation, then the command FindRoot[ f(x) -c,{x,r}] will aproximate the desired root. This command is actually performing many iterations of Newton's Method.

FindRoot[f[x]-2,{x,-5}]

FindRoot[f[x]-2,{x,-3.5}]

FindRoot[f[x]-2,{x,0}]

FindRoot[f[x]-2,{x,3}]

We thereby obtain approximations of the four roots.


Exercises

1. Find the exact solutions for x :

(i) x^3 - 5 x^2 - 5 x - 6 = 0
(ii) (x - 4)^(1/2) + 3 x = 15.

2. Find the numerical values of the algebraic solutions for x :

(i) x^5 - 8 x^4 + 3 x^2 - 2 x = -4
(ii) 3 x^4 - 4 x^3 = 5

3. Approximate all solutions to the equations : (Note : The built-in trigonometric functions are written in Mathematica as Cos[x] and Sin[x].)

(i) 0.6 x^3 - cosx = 3
(ii) sin(2x) - sin(x) + .5 x = - .25


This material was written by David K. Neal, Department of Mathematics, Western Kentucky University, Bowling Green, KY 42101 : nealdk@wku.edu.


Return to WKU Math Department.

Return to Western Online.