MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: express a quadratic, in the form (x+a)^2 + b

  • To: mathgroup at smc.vnet.net
  • Subject: [mg84360] Re: express a quadratic, in the form (x+a)^2 + b
  • From: "David Park" <djmpark at comcast.net>
  • Date: Thu, 20 Dec 2007 00:07:51 -0500 (EST)
  • References: <fkan3b$d6k$1@smc.vnet.net>

Joe,

The following is a step by step derivation of the 'complete the square' 
process. Copy the entire statement from the posting and paste into a 
notebook and then evaluate. I think you will find the steps straightforward, 
but as a beginner you will probably find the Mathematica implementation of 
the steps somewhat difficult. Nevertheless this does show how you can carry 
out derivations with Mathematica and let it do all of the calculation. You 
don't have to do anything 'by hand' on the side. I used a pure function, # - 
Part[%%, 2] &, with Map, /@,  to move the right hand side of the equation to 
the left. Pure functions (Function in Help) are extremely useful in 
manipulating expressions. I didn't actually have to do the step where the 
terms were collected but I included it because it clarifies the derivation. 
You should look up the various commands (Part, Function, Map, MapAt, 
Collect, CoefficientList and Thread) if you are unfamiliar with them. This 
is all done in a single cell using % and %% to refer back to previous 
outputs in the cell. In making such a derivation one would usually develop 
it step by step, making additions and modifications and continually 
reevaluating until you are satisfied. The Print statements aren't really 
necessary but I often find them useful for keeping track of what I'm doing.

Print["General polynomial represented as a square plus a constant"]
step1 = a x^2 + b x + c == d (x + e)^2 + f
Print["Moving right hand side to the left"]
# - Part[%%, 2] & /@ %%
Print["Collecting terms"]
MapAt[Collect[#, x] &, %%, 1]
Print["Setting the coefficients of each power of x to zero and \
threading"]
MapAt[CoefficientList[#, x] &, %%, 1]
Thread[%]
Print["Solve the equations for d, e and f"]
defsols = First@Solve[%%, {d, e, f}]
Print["Substitute into the right hand side of step1"]
step2 = Last[step1] /. defsols

Now that we have a general solution, we can write a general routine 
CompleteTheSquare.

CompleteTheSquare::usage =
  "CompleteTheSquare[x][expr] will complete the square of \
subexpressions a \!\(\*SuperscriptBox[\"x\", \"2\"]\) + b x + c in \
expr.";
CompleteTheSquare[var_: Global`x][expr_] :=
 expr //.
  a_. var^2 + b_. var + c_. -> (-b^2 + 4 a c)/(4 a) +
    a (b/(2 a) + var)^2

If you write a nice routine you should always consider writing a good usage 
message for it. All the funny stuff in the usage message is because I used a 
superscript expression in a String expression and the internal 
representation came out when I copied and pasted to this posting. In the 
routine itself we just use a Rule to replace quadratic expressions with 
their 'complete the square' forms. We allow the variable to be specified and 
give it a default value of x. Global`x is used just in case you later 
decided to copy this function into a package and the routine would have to 
know that x is the global symbol and not some internal package symbol.

You can check the usage message with:

?CompleteTheSquare

We check the routine on your initial example:

x^2+4x+7//CompleteTheSquare[]

and a more complicated example with x and y polynomials.

(3 x^2 - 11 x + 7)/(x^2 + 5 x - 11) == 5 y^2 + 24 y - 3
% // CompleteTheSquare[]
% // CompleteTheSquare[y]

You can use Mathematica to derive results. Then you can use the result of 
the derivation to write a useful routine. Then you could keep that routine, 
say in a Routines Section at the top of your notebook, or later in a 
package, and use it whenever you need it. You will have actually generated 
something useful and permanent - for yourself and perhaps for others.


-- 
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/


"Joseph Fagan" <noemailplease at nowhere.ru> wrote in message 
news:fkan3b$d6k$1 at smc.vnet.net...
> I'm a Mathematica beginner.
> I wish to express a quadratic, in the form (x+a)^2 + b
> eg to express x^2 + 4x + 7 as (x+2)^2 + 3
> Is there an easy way?
> Thanks
> Joe
>
>
> 



  • Prev by Date: Re: Re: Speeding Up Realtime Visualization
  • Next by Date: RE: LinearSolve[m, b] is not equivalent to LinearSolve[m][b]
  • Previous by thread: Re: express a quadratic, in the form (x+a)^2 + b
  • Next by thread: Plot without a graph