Section 4: Solving Equations
© 2000 Seattle Central Community College. Reproduced with permission
In this section you will learn how to apply Maple's solve( ) command to find the exact solutions of equations (when this is possible). You may recall from Precalculus that we are not able in many cases to find exact solutions to equations and so we rely on numerical solvers to find approximate solutions. Later in this section you will use Maple's fsolve( ) command to find decimal approximations for solutions. The solution to linear systems of equations will also be discussed.
> restart:
> with(plots):
Warning, the name changecoords has been redefined
Entering and Manipulating Equations: The lhs( ) and rhs( ) commands
Example 1:
Recall that we can give a name to an entire equation just as we have done for expressions.
On the next line we enter the equation
and give it the name "eqn1" .
> eqn1:=x^3-5*x^2+23=2*x^2+4*x-8;
Example 2:
We can isolate the left-hand and right-hand sides of the equation by using the lhs( ) and rhs( ) commands.
> lhs(eqn1);
> rhs(eqn1);
Example 3 :
Use the lhs( ) and rhs( ) commands to find an equation that is equivalent to the original equation eqn1 but has zero on the right-hand side. Label the new equation eqn2.
> eqn2:=lhs(eqn1)-rhs(eqn1)=0;
Finding Exact Solutions: The solve( ) command
We first consider polynomial equations. Algorithms exist for calculating the exact solutions for polynomial equations up to degree 4 . Maple's solve( ) command implements these algorithms.
Example 1:
To find the exact solutions to the polynomial equation
use the
solve( )
command.
Note that the second argument of the command tells Maple that x is the unknown variable that we are solving for.
> solve(3*x^3-4*x^2-43*x+84=0,x);
Here Maple has found all three solutions and listed them for you.
Example 2:
Sometimes you will want to select one solution from the list of solutions and use it in another computation. You can do this by first assigning a name (we use the letter N in this case) to the output of the solve( ) command. Then N[1] is the first number in the list, N[2] is the second number and so on. Note the square brackets.
> N:=solve(x^2-5*x+3=0,x);
> N[1];
Example 3:
When working with the solve( ) command it is often convenient to begin by giving a name to the equation.
Note we use " := " to assign the name and just "=" for the equation itself.
> eqn1:=7*x^3-11*x^2-27*x-9=0;
Next we solve the equation using the solve( ) command assigning the name H to the output.
> H:=solve(eqn1,x);
For practice let's check that each of these values satisfies the equation. This is easy to do using the subs( ) command.
> subs(x=H[1],eqn1);
> subs(x=H[2],eqn1);
> subs(x=H[3],eqn1);
Example 4:
Sometimes the "exact" solutions are too cumbersome to be of much use. In the next two lines we solve the equation
.
> eqn1:=x^3-34*x^2+4=0;
> H:=solve(eqn1,x);
As you can see, reading these exact solutions is quite a challenge! Note that the
I
stands for
. When a solution is this complicated it is more useful to look at the approximate solutions using
evalf( )
: .
> evalf(H);
A good alternative to the solve( ) command in a situation like this is the fsolve( ) command which will be discussed in the next section.
The solve( ) command can also be used to find the exact solutions for non-polynomial equations. Some simple examples are listed below. However if the equations are at all complicated, for example combining exponential, polynomial and trigonometric expressions, then an exact solution will typically not be available. Again the fsolve( ) command is an alternative.
Example 5:
Solve the equation:
> solve(5*exp(x/4)=43,x);
Example 6:
Sometimes Maple does not display all of the solutions. How would you use the result below to write down the entire set of solutions to the equation?
> solve(sin(x)=1/2,x);
Exercise 4.1
Solve the equation
Why does Maple produce only two distinct solutions for this cubic equation? Why is one of them written twice? (HINT: Factor the left hand side of the equation.)
Student Workspace 4.1
>
>
>
>
>
Answer 4.1
> solve(x^3-11*x^2+7*x+147=0,x);
> factor(x^3-11*x^2+7*x+147);
The fact that x - 7 is a repeated factor results in the cubic equation having only two distinct roots, -3 and 7. We say that root 7 has a multiplicity of 2 , meaning there are two factors of (x - 7) in the factored polynomial.
Finding Approximate Solutions: The fsolve( ) command
Maple's fsolve( ) command can be used to find approximate solutions for any equation. For polynomial equations fsolve() produces a complete list of all of the real solutions in one step (see Example 1). For other equations fsolve( ) can be used to get one solution at a time (see Examples 2 and 3).
Example 1:
Maple's
fsolve( )
command will compute a numerical approximation for each of the real solutions of a polynomial equation. Approximate all real solutions for the equation:
.
> eqn:=x^4-x^3-17*x^2-6*x+2=0;
> fsolve(eqn,x);
The four solutions listed above provide us with a complete list of the solutions to the given polynomial equation.
Example 2:
Find
all
real solutions of the equation
using the
fsolve( )
command.
> eqn:=x^3+1-exp(x)=0;
> fsolve(eqn,x);
Maple returns one real solution. This time Maple has not given us the whole story. Are there any other solutions? How do we find them? A systematic procedure for finding the remaining solutions is presented in Example 3.
Example 3:
Find the other real solutions for the equation
.
The first step in finding the other solutions is to plot a graph of the left-hand side of the equation.
Key Concept
: Recall that the
x-intercepts
of
correspond exactly to the solutions of the equation
.
> plot(x^3+1-exp(x),x=-3..5,y=-5..15);
The graph shows four x-intercepts. One of these corresponds to the solution we found in Example 2. Which one?
The x=0 solution is also easy to spot. How do we find the other three?
We can extend the
fsolve( )
command to look for a solution in a particular interval. For example to find the negative solution we ask Maple to search on the interval
since we can see from the graph that there definitely is one (and only one) solution on that interval.
> fsolve(eqn,x=-1..-.2);
To find the other two solutions we use fsolve( ) again, this time with search interval [1,2] and then with interval [4,5].
>
fsolve(eqn,x=1..2);
fsolve(eqn,x=4..5);
What happens if you ask Maple to search for a solution on an interval where no solution exists ?
Let's try it out. From the graph it is clear that there are no x-intercepts (and therefore no solutions) between 2 and 4.
> fsolve(eqn,x=2..4);
Notice that Maple simply returns the original input line unchanged when it cannot find a solution on the given interval.
Are there any other solutions? For example, are there any solutions larger than 5 ? We can check this out by expanding the interval over which the graph is plotted. On the next line we expand the interval to [-3,50]. No other x-intercepts appear. The graph confirms what we should expect by looking at the terms of the expression, namely the exponential term dominates and causes the graph to go down in the long run.
> plot(x^3+1-exp(x),x=-3..50,y=-10..15);
Alternatively we can use the fsolve( ) command, now searching over this larger interval.
> fsolve(eqn,x=5..50);
As expected no solutions are found by Maple.
In a similar way we can check for solutions to the left . Here we search for solutions over the interval [-50,-1] .
> fsolve(eqn,x=-50..-1);
None there either !
We now have a complete list of the four solutions of our original equation
. They are: -.8251554597 , 0 , 1.545007279 and 4.567036837
Example 4:
Use
fsolve( )
to find the approximate solutions of the equation :
Just as in the last example we will use a graph to help us determine the number and approximate location of the solutions. Our task is simplified if we start by converting the given equation to an equivalent one that has zero on the right-hand side.
So we will solve the equivalent equation :
If we now graph the left-hand side of this equation we once again will find solutions at each of the x-intercepts.
> eqn:=x^2/20-10*x-15*cos(x+15)=0;
> plot(lhs(eqn),x=-10..10);
From the graph it appears that there is a solution on the interval [1,2].
We now direct Maple to search for a solution on this interval.
> fsolve(eqn,x=1..2);
Have we found all of the solutions to this equation? In fact there is another solution! To find it start by expanding the interval over which the graph is drawn. Then use fsolve( ) to find a numerical approximation for this second solution.
Exercise 4.2
Find all the solutions to the equation
. Begin by looking at a relevant graph.
Student Workspace 4.2
>
>
>
>
>
Answer 4.2
We begin by graphing the left-hand side of the equation.
> eqn:=x^5-4*x^3+3*x^2+7*x-1=0;
> plot(lhs(eqn),x=-5..5,y=-5..5);
This picture indicates that there are solutions near -2 , -1.5, and 0. We next try the unrestricted fsolve command to see which solution(s) Maple finds.
> fsolve(eqn,x);
Since this is a polynomial equation the fsolve( ) command gives us a complete list of the real solutions.
Exercise 4.3
Find all the solutions to the equation
. Use the graph of
one
expression to locate the solutions. Check each solution by substituting it back in the original equation.
Student Workspace 4.3
>
>
>
>
>
>
>
Answer 4.3
First we put the equation in "standard form" , i.e. with zero on one side.
> eqn:=x^2-2-ln(x+5)=0;
Now we can graph the right-hand side equation.
> plot(lhs(eqn),x=-10..10);
There appear to be two solutions. One near -2 and the other near 2. Use fsolve with a restricted domain to find the two solutions you've located more precisely.
> soln1:=fsolve(eqn,x=-5..0);
> soln2:=fsolve(eqn,x=1..3);
Check by subsituting back into the original equation.
> evalf(subs(x=soln1,eqn));
> evalf(subs(x=soln2,eqn));
Notice that the original equation is only "approximately" satisfied by each of our solutions. The slight discrepancy is a result of round-off error in the approximate solutions.
Check that there are no additional solutions.
Exercise 4.4
The graphs of
and
intersect twice on the interval [-5,5].
a) Graph the two equations together and estimate the intersection points using the mouse.
b) Write an equation that can be solved to find the x-coordinates of the intersection points.
c) Use fsolve( ) to solve this equation.
d) Use the results from part c) to estimate the y-coordinates of the intersection points.
e) It appears that the curves may intersect at a third point near (1,9). Use fsolve( ) and/or a relevant graph to demonstrate that there is no intersection point at that location.
Student Workspace 4.4
>
>
>
>
>
>
>
Answer 4.4
> y1:=10-x^2;
> y2:=4*sin(2*x)+5;
Here is a plot of the two equations.
> plot([y1,y2],x=-5..5);
Intersection points are located approximately at: (-1.8, 6.6) and ( 2.75, 2) .
b) The equation to solve is y1=y2.
> eqn:= y1=y2;
c) We now find the two solutions using fsolve( )
> x_soln1:=fsolve(y1=y2,x=-4..0);
> x_soln2:=fsolve(y1=y2,x=0..4);
d) We can use the subs( ) command to find the corresponding y-coordinates.
> y_soln1:=subs(x=x_soln1,y1);
> y_soln2:=subs(x=x_soln2,y1);
So the points of intersection are : (-1.800,6.763) and (2.773,2.311)
e) Here is a closer look at what is happening near x=1.
> plot([y1,y2],x=.5..1.5);
Alternatively we can use fsolve( ) to confirm that there is no solution near x=1.
> fsolve(y1=y2,x=.5..1.5);
Solving Literal Equations
> restart:
Often Maple can solve literal equations for any one of the variables.
Suppose we want to solve for the variable g in the equation:
.
The solve command works well here.
> solve(4-v=2*T-k*g,g);
Here is a little nicer way of displaying the same result:
> g=solve(4-v=2*T-k*g,g);
Exercise 4.4
Edit the last command to solve for each of the other letters T, k and v.
Student Workspace 4.4
>
>
>
>
>
Answer 4.4
> T=solve(4-v=2*T-k*g,T);
> k=solve(4-v=2*T-k*g,k);
> v=solve(4-v=2*T-k*g,v);
Exercise 4.5
Solve the equation
for
y
. Assign the set of solutions to a variable named S. How are the two solutions S[1] and S[2] related ?
Student Workspace 4.5
>
>
>
>
>
Answer 4.5
> S:=solve(x^2+y^2=25,y);
> S[1];
> S[2];
The solution S[1] is the negative of solution S[2].
Solving a Linear System of Equations using the solve( ) command
Please execute the next two lines before proceeding:
> restart:
> with(plots):
Warning, the name changecoords has been redefined
The solve( ) command can also be used to solve a system of m linear equations in n variables. We call these m by n linear systems for short.
Example 1:
Solve the 2 by 2 system:
and
> solve({3*x+2*y=3,x-y=-4});
A graph of the two underlying functions shows the solution corresponds to the point of intersection at (-1,3). But we first need to find the explicit form for each of the linear functions before we can graph them. So we solve each equation for y.
> y1:=solve(3*x+2*y=3,y);
> y2:=solve(x-y=-4,y);
Now we construct a picture made up of two parts: "part1" contains the graphs the two equations and "part2" plots the solution point that we found. This point should be the intersection point of the two lines. Is it ?
> part1:=plot([y1,y2],x=-5..5):
> part2:=plot([[-1,3]],style=point,color=blue,symbol=circle):
> display([part1,part2]);
Example 2:
Here is an example of the solution of a 3 by 3 system with variables x, y, and z.
Solve the 3 by 3 system:
> solve({x+y+z=1, 3*x+y=3, x-2*y-z=0});
Exercise 4.6
Find the solution to the system:
Check by subsitituting the solution pair in both equations in the system
Student Workspace 4.6
>
>
>
>
>
>
Answer 4.6
> eqns:={4*x+3*y=12, 5*x-7*y=35};
> ans:=solve(eqns);
> subs(x=189/43,y=-80/43,eqns);
Linear Systems with an Infinite Number of Solutions (Optional)
When a system has more variables than equations we often get not one, but an infinite number of solutions.
Here is an example.
Example 1:
Solve the system :
.
> solns:=solve({x+y+z=1, 3*x+y=3});
Notice this time we do not get a single set of numerical values for x, y and z. Instead Maple tells us how the values of x, y and z must be related to construct a typical solution.
In particular the expression
in the output above indicates that
can be
any
number. We refer to it as the "free" variable in the solution. To find any
particular
solution (among the infinte number possible) pick any value for x and use this to calculate the corresponding values for y and z. For example let x = 4.
> subs(x=4,solns);
So one solution is : x=4 , y= -9 and z=6. Take a minute and check by hand that these three numbers do in fact satisfy our original equations :
and
.
Now let's look at the solution that is generated when we take x=2.
> subs(x=2,solns);
So two of the infinitely many solutions are: (x,y,z) = (4, -9, 6) and (2, -3, 2).
Exercise 4.7
Solve the system :
and find at least three particular solutions.
Student Workspace 4.7
>
>
>
>
>
Answer 4.7
> eqns:={ x+2*y+z=2 , 3*x+y=1 };
> soln:=solve(eqns);
Here are the solutions for x = 1, 2, 3, and 4 :
> subs(x=1,soln);
> subs(x=2,soln);
> subs(x=3,soln);
> subs(x=4,soln);
Section 5: Functions: Defining, Evaluating and Graphing