In the linear algebra chapter you saw how to solve systems of linear equations. These are relatively easy, but Maple can handle much harder non-linear equations as well. In this chapter we will learn how to find the roots of polynomials, solve impossible equations involving both elementary and special functions, and even handle sets of impossible equations.
Equations in a single variable
> restart;
> s1:=solve(a*x^2+b*x+c=0,x);
> s2:=solve(x^2-8*x+14=0,x);
Maple even knows the formula for the cubic equation
> s3:=solve(a*x^3+b*x^2+c*x+d=0,x);
but it is so ugly that it is almost never used. Instead we give numerical coefficients and expect a numerical answer
> s4:=solve(x^3+3*x^2-2*x+7=0,x);
Well, these are numbers correct, but because Maple was thinking symbolically, they don't look very useful. To get actual numbers you can either use our old friend evalf
> evalf(s4);
or enter the coefficients in the equation with decimal points.
> s4:=solve(x^3+3.*x^2-2.*x+7.=0,x);
The
solve
command with polynomials is smart: it knows the fundamental theorem of algebra, which is that an
order polynomial has
roots. For instance if you ask it for the roots of x^6+1 this way
> s5:=evalf(solve(x^6+1=0,x));
It will give you all 6. Let's try another one
> s6:=solve(x^6+3*x^5+2=0,x);
You will be seeing RootOf a lot as you solve equations in Maple. The problem is that you gave Maple a polynomial with integer coefficients, which tells Maple to try to solve the equation with radicals (square roots, cube roots, etc.). And in this case it couldn't do it, except for the root -1. But if you just want numbers, either use evalf , or change the coefficients to floating point numbers by adding decimal points and you will get all 6.
> evalf(s6);
> solve(x^6+3.*x^5+2.=0,x);
Maple can also handle equations involving harder functions, like this one:
.
> s7:=solve(cos(x)-x/10=0,x);
RootOf again, and the solution is the same: change the 10 to 10., or use evalf(s7) , to find at least one of the solutions.
> evalf(s7);
But when you solve hard equations like this you need to be careful, because there may be more than one answer and Maple will not give you all of them. Let's try plotting the function
and see how many zero-crossings it has to see how many answers we should have found.
> plot(cos(x)-x/10,x=-15..15);
>
So Maple didn't actually lie, it just didn't volunteer information. 1.427551779 is a solution, but there are others as well. This is important: whenever possible, make a plot first so you know how many solutions you are looking for.
> fsolve(cos(x)-x/10=0.,x,-10..-9.5);
> fsolve(cos(x)-x/10=0.,x,-9.5..-8.8);
etc..
Notice what happens if you accidentally specify a range in which there is no solution:
> fsolve(cos(x)-x/10=0.,x,-8.5..-8);
In general, when Maple just gives back what you gave it, it means either it couldn't find the answer, or you loused up. It's your job to figure out which it is.
Notice also that you can just give it a single guess at where the solution is instead of a range
> fsolve(cos(x)-x/10=0.,x=1.5);
But be careful here. If you give fsolve an initial guess that is close to a maximum of the function whose zeros you are looking for fsolve might get lost, or at least go find a solution that is nowhere your initial guess. When you study the secant method in Chapter 8 you will see why this happens.
> fsolve(cos(x)-x/10=0.,x=0);
Yet another way of controlling which solution Maple gives you is the
avoid
option in
fsolve
. Suppose you are looking for solutions of the equation
. If you plot this function to see where the zeros are, you will see two closely spaced solutions just below 3. This command will find one of them:
> s1:=fsolve(cos(x)+x/3=0,x=3);
but maybe you wanted the next one down and it is hard to tell from the graph what initial guess or range to use to get it. Well, you can tell Maple to find another one near 3, but not to find s1 again, like this
> s2:=fsolve(cos(x)+x/3=0,x=.09,avoid={x=s1});
>
Problem 6.1
Find all of the roots, real and complex, of the following polynomials. Use both solve and fsolve on them. You will notice that solve will give you complex values, but fsolve won't. You can fix this by telling fsolve that you want complex solutions, like this: fsolve(x^2+1=0,x,complex) . Also use the command factor(f,complex) as well, where f is the polynomial you want to factor.
>
(a)
(b)
----------------------------------------------------------------------------
Problem 6.2
Find all of the solutions between -5 and 20 of the equation
.
----------------------------------------------------------------------------
Problem 6.3
Find all of the solutions of the equation
. Use both
solve
and
fsolve
and compare their answers.
---------------------------------------------------------------------------
As a first example, let's have solve do a simple linear algebra problem like the ones in Chapter 5. John is twice as old as Kimberly. Kimberly's age added to John's age is 27. Find their ages. Using the LinearAlgebra package we would set this up as a matrix, but solve ( and fsolve ) can work with the equations directly, like this:
> restart;
> E1:=J=2*K;E2:=K+J=27;
> solve({E1,E2},{J,K});
> fsolve({E1,E2},{J,K});
Because you don't have to worry about the conversion to matrix form, this is probably the method of choice for solving systems of linear equations.
But solve and fsolve can also solve nonlinear systems, i.e., system of equations in which the variables are squared, cubed, sit inside sines, cosines, exponentials, etc.. For example, here is a set of two nonlinear equations to solve
> restart;
> E1:=x^2-y=5;E2:=x-y^2=-13;
Let's try the solve command first
> s:=solve({E1,E2},{x,y});
Maple was smart; it used equation E1 to eliminate y from equation E2 to get a single equation for x. Then it factored this quartic equation to pull out the answer (x,y)=(3,4) and reduced the rest of the problem to a cubic. If we let evalf finish the problem it gives us this
> evalf(s);
Well, that didn't get us anywhere. Now go back up and replace the integers 5 and 13 in E1 and E2 with floating point numbers 5. and 13.; then execute the code above starting at the restart and see what happens. There is a lesson here: when Maple doesn't give you what you want, be creative. Try alternate forms of the commands and maybe you will get lucky.
>
Problem 6.4
Find all of the solutions (real and complex) of the set of equations
--------------------------------------------------------------------------
Here's another nonlinear set to put Maple to the test.
> restart;
> E1:=cos(x)+y-sqrt(z)=-.191748502;E2:=x*y*z=3;E3:=x+y+2*z=8;
Let's try solve first
> solve({E1,E2,E3},{x,y,z});
Well, that was informative. Now let's try fsolve with specified ranges for each variable
> fsolve({E1,E2,E3},{x,y,z},{x=-3..3,y=-3..3,z=-3..3});
>
so it looks like (x,y,z)=(1,1,3) is pretty close to a solution. I warn you, however, that in 3 dimensions and higher Maple can just get lost and run forever unless (a) you know there is a solution and (b) you tell Maple approximately where to look.
Problem 6.5
A particle finds itself in a region of space where its potential energy function is given by the formula
. By using the fact that the force on the particle is given by
, find 4 points of equilibrium. (Finding just one will be pretty easy--after that you will have to use the range option in
fsolve
with some creativity to avoid finding the same ones over and over.) To see how to take the gradient in Maple, use
> ?grad
and rummage through the help page until you can see how to apply it to this problem. Remember: the examples at the bottom are your friends. Warning: do not use the syntax -grad(U) in this problem. If you do, you will get a weird answer back that is hard to deal with. Instead use grad(-U) . The problem is subtle and involves how vectors are handled in the linalg package. You could get the -grad(U) form to work if you used evalm(-grad(U)) .