#Solving Equations

# Consider some examples:

 solve( x^2 + 3*x = 2.1 );
 solve( x^3 + x = 27 ):    # A complicated answer!
 solve( x^3 + x = 27.0 );

# The second command gives an exact, but complicated, answer.  Replacing
# 27 by 27.0 forces Maple to give decimal approximations instead, as do
# the commands

 fsolve( x^3 + x = 27 );
 fsolve( x^3 + x=27, x, complex);

# In general, solve looks for exact answers using algebraic methods,
# whereas fsolve uses numberical methods to find approximate solutions in
# floating-point form.  Compare

 solve( tan(x) - x = 2 );
 fsolve( tan(x) - x = 2 );

# Maple responds with an echo of the failed command, or a blank line, if
# it cannot find the solution you asked for. Command fsolve may not find
# all solutions.  To understand why not, it is helpful to look at a graph


 plot( { tan(x) - x, 2 }, x = 0..10, y = -10..10 );

# This will give you an idea of how many solutions there are and what
# their approxmiate location is.  Then give fsolve a range of x -values
# in which to search:

 fsolve( tan(x) - x = 2, x = 4..5 );

# Often we need to use the solution of an equation in a later problem. To
# do this, assign a name to it.  Here is one example.

 r := solve( x^2 + 3*x -2.1 = 0 );

# The answer has the form r := r1, r2 , where r1 is the first root and
# r2 is the second.  Such an object - a bunch of items separated by
# commas, is called an expression sequence.  Items of an expression
# sequence are extracted this way:

 r[1];
 r[2];

# Here are some computations with items from an expression sequence:

 r[1] + r[2];                # sum of the roots
 r[1]*r[2];                  # their product
 subs( x = r[1], 2*x + 3 );  # find 2(first solution) + 3

# We can also solve systems of equations:

 solve( { 2*x + 3*y = 1, 5*x + 7*y = 2 },[x,y] );
 x; y; # Surprise: symbols x,y unassigned by solve()
 
# A system of equations is given as a set - a bunch of items enclosed in
# curly brackets and separated by commas.  Sets are often used when the
# order of the objects is unimportant.  In reply to the solve command
# above, Maple tells us how to choose x and y to solve the system, but it
# does not give x and y these particular values.  To force it to assign
# these values, we use the assign function:

 s := solve( { 2*x + 3*y = 1, 5*x + 7*y = 2 }, [x,y] );
 assign( s );
 x; y;      #check that it worked

# Important note: To type multiple line commands, as above, use
# Shift-Return instead of Return.  And a warning: You may have trouble
# later if you leave numerical values assigned to the variables x , y ,
# and r .  Maple will not forget these assigned values, even though you
# have gone on to a new problem where x means something different.  It is
# a good idea to return variables to their unassigned state when you
# finish your problem.

 x := 'x'; y := 'y'; r := 'r'; # or, unassign('x','y','r'): 

# Recall that restart also clears all variables.

# Finally, symbolic parameters are allowed in solve commands.  However,
# in that case we have to tell Maple which ones to solve for and which
# ones to treat as unspecified constants:

 solve( a*x^2 + b*x + c, x );       # solve ax^2+bx+c=0 for x
 solve( a*x^3 + b*x^2 + c*x +d, x):

 solve( { a*x + b*y = h, c*x + d*y = k }, [ x,y ] ):     
   # solving a system for x and y. Wrong answer for ad-bc=0. 
   # Maple engine makes undisclosed assumptions. 
   # Help ?solve explains why.