# Manipulating Expressions

Confirm that applying an of the following commands to the 
expression (x+2)^2*(x-5)^3  has the same effect: expand,
ratsimp, and radcan. (Don't assume that the three always yield identical 
results. They do not.)  Bind one of the resulting expressions to the name
poly5 (the result is a fifth-degree polynomial).

/*Input. Execute (ctrl-enter) for results */
kill(all)$
poly5: expand((x+2)^2*(x-5)^3);
ratsimp((x+2)^2*(x-5)^3);
radcan((x+2)^2*(x-5)^3);

Confirm that factor(poly5); generates the original expression.

/*Input. Execute (ctrl-enter) for results */
factor(poly5);

Use solve to confirm that x=-2 and x=5 are the solutions for poly5.

/*Input. Execute (ctrl-enter) for results */
solve(poly5,x);

Apply subst twice to confirm that poly5 = -9000 when 
x = -5 and poly5 = 2700 when x = 8.

/*Input. Execute (ctrl-enter) for results */
[subst(8,x,poly5), subst(-5,x,poly5)];

wxdraw2d(yrange=[-800, 500],xaxis=true,
explicit(poly5,x,-5,8))$

TeX:
The value of $a$ that is stored in \emph{Maxima}'s memory is not 
changed by \verb!ratsimp(a)!, as output \verb!%o3! in the cell above shows.
The \verb!ratsimp! command puts an expression into the form of 
numerator over denominator and divides out the greatest 
common divisor of numerator and denominator, as the example below illustrates.

/*Input. Execute (ctrl-enter) for results */
kill(all)$
poly5: expand((x+2)^2*(x-5)^3) +50;
ratsimp((x+2)^2*(x-5)^3) + 50;
radcan((x+2)^2*(x-5)^3)+50;

wxdraw2d(yrange=[-1000, 1000],xaxis=true,
explicit(poly5,x,-5,8))$

Confirm that factor( ) does not, in fact, succeed in factoring this expression

/*Input. Execute (ctrl-enter) for results */
factor(poly5);

Confirm that solve( ) does not yield an analytical solution to poly5=0.

/*Input. Execute (ctrl-enter) for results */
solve(poly5=0,x);

The allroots command, not used in the text, applies to polynomials and 
does what its name indicates.

allroots(poly5);

Confirm that find_root, used three times, generates the three real
roots that are listed above. Use the graph or the output in the 
previous cell to guide your selection of end points for find_root.

/*Input. Execute (ctrl-enter) for results */
[find_root(poly5,x,-8,-2),
find_root(poly5,x,-2,0),
find_root(poly5,x,0,10)];

Apply the kill(all) command and then create a new poly5 using this 
command: poly5: expand(((x+2)^2*(x-5)^3)/((x-3)*(x+4)));. Then 
apply either ratsimp( ) or radcan( ) to the result.

/*Input. Execute (ctrl-enter) for results */
kill(all)$
poly5: expand(((x+2)^2*(x-5)^3)/((x-3)*(x+4)));
ratsimp(((x+2)^2*(x-5)^3)/((x-3)*(x+4)));
radcan(((x+2)^2*(x-5)^3)/((x-3)*(x+4)));

wxdraw2d(yrange=[-200, 200],xaxis=true,
explicit(poly5,x,-5,8))$

For which two values of x is this rational expression not
defned? Explain.

Apply factor( ) to poly5.

/*Input. Execute (ctrl-enter) for results */
factor(poly5);

Confirm that solve( ) yields two solutions for poly5=0.

/*Input. Execute (ctrl-enter) for results */
solve(poly5,x);

Confirm that allroots does not apply to this rational
expression, which is not polynomial.

/*Input. Execute (ctrl-enter) for results */
allroots(poly5);

Confirm that find_root cannot be applied to find the root
at x = -2. The Maxima output explains why this is so.

/*Input. Execute (ctrl-enter) for results */
find_root(poly5,x, -3,0);

Use find_root to determine that x = 5.0 is a root. Choose your 
endpoints carefully, referring to the graph above.

/*Input. Execute (ctrl-enter) for results */
find_root(poly5,x, 4,10);

Try to apply find_root with these end points: x=-5 and x=-3, 
and then with x=2 and x = 4.5. Why can find_root not work 
over these two ranges of x?

/*Input. Execute (ctrl-enter) for results */
find_root(poly5,x, -5, -3);
find_root(poly5,x, 2, 4.5);
/*Returning a command without carrying out
the operation usually indicates that the 
command makes no sense to Maxima. Sometimes
this is because the command has been entered
wrong, e. g., due to a typegraphical error.
Here, Maxima repeats the command, clearly 
recognizing what poly5 is. It cannot carry 
out the operation because the specified ranges
include values for which poly5 is not defined.
*/
end;

Create an expression named mixed that contains a simple polynomial
and a term with a negative exponent, 1.5.
mixed : 40 - x - 1200*x^-1.5;

/*Input. Execute (ctrl-enter) for results */
kill(all)$
mixed : 40 - x - 1200*x^-2;

parts: gr2d(yrange = [-40,40],xaxis=true,
ytics=20,draw_realpart=true,
explicit(1200*x^-2,x,0,50), explicit(40-x,x,0,50))$
whole: gr2d(yrange=[-40,25],ytics=20,xaxis=true,
draw_realpart=true,explicit(mixed,x,0.01,50) )$
wxdraw(parts,whole);

Confirm that solve(mixed,x); does not yield useful results. 
Specifically, all results appear as complex numbers, even though
the graphs above show that two real solutions occur.

/*Input. Execute (ctrl-enter) for results */
transpose(matrix(solve(mixed, x)));
/*
transpose(matrix(...)) used to make result
easier to read. It is not required.
*/
end;

Use newton1 to confirm that that two real solutions exist, one near 5.9 and 
the other near 39.2. Doing this requires three commands: load(newton1)$ and 
two of the form newton(mixed,x,guess,iteration size);. 

Increase floating-point print precision to 16.

/*Input. Execute (ctrl-enter) for results */
fpprintprec:16$
load(newton1)$
newton(mixed,x,5.9,0.001);
newton(mixed,x,39.2,.001);

Confirm that find_root yields values close but not identical to those
derived above.

/*Input. Execute (ctrl-enter) for results */
find_root(mixed,x,0.01,10);
find_root(mixed,x,10,100);