(*^
::[ Information =
"This is a Mathematica Notebook file. It contains ASCII text, and can be
transferred by email, ftp, or other text-file transfer utility. It should
be read or edited using a copy of Mathematica or MathReader. If you
received this as email, use your mail application or copy/paste to save
everything from the line containing (*^ down to the line containing ^*)
into a plain text file. On some systems you may have to give the file a
name ending with ".ma" to allow Mathematica to recognize it as a Notebook.
The line below identifies what version of Mathematica created this file,
but it can be opened using any other version as well.";
FrontEndVersion = "NeXT Mathematica Notebook Front End Version 2.2";
NeXTStandardFontEncoding;
fontset = title, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, L1, e8, 24, "Times"; ;
fontset = subtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, L1, e6, 18, "Times"; ;
fontset = subsubtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, L1, e6, 14, "Times"; ;
fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, L1, a20, 18, "Times"; ;
fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, L1, a15, 14, "Times"; ;
fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, L1, a12, 12, "Times"; ;
fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12;
fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 10, "Times"; ;
fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, L1, 12, "Courier"; ;
fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5, 12, "Courier"; ;
fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1, 12, "Courier"; ;
fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1, 12, "Courier"; ;
fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1, 12, "Courier"; ;
fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287, L1, 12, "Courier"; ;
fontset = name, inactive, noPageBreakInGroup, nohscroll, preserveAspect, M7, italic, B65535, L1, 10, "Times"; ;
fontset = header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, L1, 12, "Times"; ;
fontset = leftheader, 12;
fontset = footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M7, italic, L1, 12, "Times"; ;
fontset = leftfooter, 12;
fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12;
fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12;
fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12, "Courier"; ;
fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12;
fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12;
fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12;
fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12;
fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12;
paletteColors = 128; currentKernel;
]
:[font = title; inactive; preserveAspect; startGroup]
Solving Quadratics Step by Step
:[font = subsubtitle; inactive; preserveAspect]
by George Beck
:[font = text; inactive; preserveAspect]
This notebook discusses the solutions of quadratic equations in a several different ways. The built-in functions Solve, NSolve, and Factor give three fast ways to solve a quadratic equation. Their solutions are easy to check. The defined function WalkSolve solves a quadratic step-by-step by completing the square. We derive the QuadraticFormula and compare it with the Russian version.
:[font = subsection; inactive; preserveAspect; startGroup]
Proper formatting of output
:[font = subsubsection; inactive; preserveAspect]
From the menu, select Info, Preferences, Action... to produce a big box titled Action Preferences.
;[s]
2:0,0;97,1;100,-1;
2:1,11,8,Times,0,12,0,0,0;1,11,8,Times,0,12,0,0,0;
:[font = subsubsection; inactive; preserveAspect]
Look at the second box within Action Preferences titled Placing Print Output.
;[s]
2:0,0;76,1;78,-1;
2:1,11,8,Times,0,12,0,0,0;1,11,8,Times,0,12,0,0,0;
:[font = subsubsection; inactive; preserveAspect]
Click on the first little button so that there is no check mark for ``Place Print output as it is generated''.
;[s]
1:0,0;111,-1;
1:1,11,8,Times,0,12,0,0,0;
:[font = subsubsection; inactive; preserveAspect]
Click on the second little button so that there is a check mark for ``Place each Print line in a separate cell''.
;[s]
1:0,0;114,-1;
1:1,11,8,Times,0,12,0,0,0;
:[font = subsubsection; inactive; preserveAspect; endGroup]
Click on the Apply button at the bottom.
;[s]
1:0,0;42,-1;
1:1,11,8,Times,0,12,0,0,0;
:[font = section; inactive; preserveAspect; startGroup]
Discussion
:[font = subsection; inactive; preserveAspect]
Quadratic expressions and equations
:[font = text; inactive; preserveAspect]
This is a quadratic expression. We'll use this example for our discussion, but you can change the numbers to get any example you want.
:[font = input; preserveAspect; startGroup]
q = 2 - 4 x + x^2
:[font = output; output; inactive; preserveAspect; endGroup]
2 - 4*x + x^2
;[o]
2
2 - 4 x + x
:[font = text; inactive; preserveAspect]
Setting a quadratic expression equal to zero gives a quadratic equation.
:[font = input; preserveAspect; startGroup]
q == 0
:[font = output; output; inactive; preserveAspect; endGroup]
2 - 4*x + x^2 == 0
;[o]
2
2 - 4 x + x == 0
:[font = subsection; inactive; preserveAspect; startGroup]
Three fast ways to solve quadratic equations
:[font = subsubsection; inactive; preserveAspect]
1) Solve
:[font = text; inactive; preserveAspect]
We can find the two solutions with the built-in function Solve.
The answers are given as rules, and x is not set to any particular value.
:[font = input; preserveAspect; startGroup]
solutionrules = Solve[q == 0, x]
:[font = output; output; inactive; preserveAspect; endGroup]
{{x -> (4 - 2*2^(1/2))/2}, {x -> (4 + 2*2^(1/2))/2}}
;[o]
4 - 2 Sqrt[2] 4 + 2 Sqrt[2]
{{x -> -------------}, {x -> -------------}}
2 2
:[font = text; inactive; preserveAspect]
Somtimes the answer can be simplified.
:[font = input; preserveAspect; startGroup]
solutionrules = Simplify[solutionrules]
:[font = output; output; inactive; preserveAspect; endGroup]
{{x -> 2 - 2^(1/2)}, {x -> 2 + 2^(1/2)}}
;[o]
{{x -> 2 - Sqrt[2]}, {x -> 2 + Sqrt[2]}}
:[font = text; inactive; preserveAspect]
If there are square roots you can get decimal approximations by using the function N, which stands for numeric .
:[font = input; preserveAspect; startGroup]
N[solutionrules]
:[font = output; output; inactive; preserveAspect; endGroup]
{{x -> 0.5857864376269049}, {x -> 3.414213562373095}}
;[o]
{{x -> 0.585786}, {x -> 3.41421}}
:[font = subsubsection; inactive; preserveAspect]
2) NSolve
:[font = text; inactive; preserveAspect]
Use NSolve if you know from the beginning that you want decimals.
:[font = input; preserveAspect; startGroup]
NSolve[q == 0, x]
:[font = output; output; inactive; preserveAspect; endGroup]
{{x -> 0.585786437626905}, {x -> 3.414213562373095}}
;[o]
{{x -> 0.585786}, {x -> 3.41421}}
:[font = subsubsection; inactive; preserveAspect]
3) Factor
:[font = text; inactive; preserveAspect]
If the solutions are rational you can get them by factoring the expression.
:[font = input; preserveAspect; startGroup]
Factor[q]
:[font = output; output; inactive; preserveAspect; endGroup]
2 - 4*x + x^2
;[o]
2
2 - 4 x + x
:[font = text; inactive; preserveAspect]
Here is a quadratic expression that factors over the integers.
:[font = input; preserveAspect; startGroup]
Factor[2 - 3 x + x^2]
:[font = output; output; inactive; preserveAspect; endGroup; endGroup]
(-2 + x)*(-1 + x)
;[o]
(-2 + x) (-1 + x)
:[font = input; preserveAspect; startGroup]
Solve[2 - 3 x + x^2 == 0, x]
:[font = output; output; inactive; preserveAspect; endGroup]
{{x -> 1}, {x -> 2}}
;[o]
{{x -> 1}, {x -> 2}}
:[font = subsection; inactive; preserveAspect; startGroup]
Checking solutions
:[font = text; inactive; preserveAspect]
The result from Solve is a pair of rules, not a pair of numbers. We can substitute for x by using slash-dot (/.), the replacement operator.
:[font = subsubsection; inactive; preserveAspect]
Checking that the expression becomes zero
:[font = input; preserveAspect; startGroup]
q /. solutionrules
:[font = output; output; inactive; preserveAspect; endGroup]
{0, 0}
;[o]
{0, 0}
:[font = subsubsection; inactive; preserveAspect]
Checking the equation is satisfied
:[font = text; inactive; preserveAspect]
The result shows that the quadratic equation q equals zero is correct for both solutions.
:[font = input; preserveAspect; startGroup]
q == 0 /. solutionrules
:[font = output; output; inactive; preserveAspect; endGroup]
{True, True}
;[o]
{True, True}
:[font = subsubsection; inactive; preserveAspect]
Getting the solutions as a list
:[font = text; inactive; preserveAspect]
To get the actual numbers rather than the rules we can substitute out for x.
:[font = input; preserveAspect; startGroup]
x /. solutionrules
:[font = output; output; inactive; preserveAspect; endGroup]
{2 - 2^(1/2), 2 + 2^(1/2)}
;[o]
{2 - Sqrt[2], 2 + Sqrt[2]}
:[font = text; inactive; preserveAspect]
Again we can get an approximation.
:[font = input; preserveAspect; startGroup]
N[%]
:[font = output; output; inactive; preserveAspect; endGroup; endGroup]
{0.5857864376269049, 3.414213562373095}
;[o]
{0.585786, 3.41421}
:[font = subsection; inactive; pageBreak; preserveAspect; startGroup]
Completing the square
:[font = text; inactive; preserveAspect]
WalkSolve finds the solutions one step at a time. Try to follow the algebra.
:[font = input; preserveAspect; startGroup]
WalkSolve[q == 0, x]
:[font = print; inactive; preserveAspect]
2
2 - 4 x + x == 0
:[font = print; inactive; preserveAspect]
2
-4 x + x == -2
:[font = print; inactive; preserveAspect]
2
4 - 4 x + x == 2
:[font = print; inactive; preserveAspect]
2
(-2 + x) == 2
:[font = print; inactive; preserveAspect]
First we'll take the positive square root.
:[font = print; inactive; preserveAspect]
-2 + x == Sqrt[2]
:[font = print; inactive; preserveAspect]
x == 2 + Sqrt[2]
:[font = print; inactive; preserveAspect]
That's the first solution.
:[font = print; inactive; preserveAspect]
Now let's take the negative square root.
:[font = print; inactive; preserveAspect]
-2 + x == -Sqrt[2]
:[font = print; inactive; preserveAspect]
x == 2 - Sqrt[2]
:[font = print; inactive; preserveAspect; endGroup; endGroup]
That's the second solution.
:[font = subsection; inactive; pageBreak; preserveAspect; startGroup]
The quadratic formula
:[font = text; inactive; preserveAspect]
Since we want to have general values for a, b, and c, we remove any values that might have been set for them earlier.
:[font = input; preserveAspect]
Clear[a, b, c]
:[font = text; inactive; preserveAspect]
This is a general quadratic expression.
:[font = input; preserveAspect; startGroup]
Q = a x ^ 2 + b x + c
:[font = output; output; inactive; preserveAspect; endGroup]
c + b*x + a*x^2
;[o]
2
c + b x + a x
:[font = text; inactive; preserveAspect]
Since we will want to divide by a, we better assume that it is not zero.
What if a is zero?
:[font = text; inactive; preserveAspect]
This is the general quadratric equation.
:[font = input; preserveAspect; startGroup]
Q == 0
:[font = output; output; inactive; preserveAspect; endGroup]
c + b*x + a*x^2 == 0
;[o]
2
c + b x + a x == 0
:[font = text; inactive; preserveAspect]
In ordinary mathematical notation there is a plus-or-minus sign in front of the square root and the formula is half as long.
:[font = input; preserveAspect; startGroup]
Solve[Q == 0, x]
:[font = output; output; inactive; preserveAspect; endGroup]
{{x -> (-b - (b^2 - 4*a*c)^(1/2))/(2*a)},
{x -> (-b + (b^2 - 4*a*c)^(1/2))/(2*a)}}
;[o]
2
-b - Sqrt[b - 4 a c]
{{x -> ---------------------},
2 a
2
-b + Sqrt[b - 4 a c]
{x -> ---------------------}}
2 a
:[font = text; inactive; preserveAspect]
Let us replace the variable x using these rules.
:[font = input; preserveAspect; startGroup]
QuadraticFormula = x /. %
:[font = output; output; inactive; preserveAspect; endGroup]
{(-b - (b^2 - 4*a*c)^(1/2))/(2*a),
(-b + (b^2 - 4*a*c)^(1/2))/(2*a)}
;[o]
2 2
-b - Sqrt[b - 4 a c] -b + Sqrt[b - 4 a c]
{---------------------, ---------------------}
2 a 2 a
:[font = subsubsection; inactive; preserveAspect]
Substitution
:[font = text; inactive; preserveAspect]
We can substitute for a, b, and c without having to give them permanent values. Feel free to use other numbers.
:[font = input; preserveAspect; startGroup]
QuadraticFormula /. {a -> 1, b -> 2, c -> 1}
:[font = output; output; inactive; preserveAspect; endGroup]
{-1, -1}
;[o]
{-1, -1}
:[font = subsection; inactive; preserveAspect]
Deriving the quadratic formula one step at a time
:[font = text; inactive; preserveAspect]
Now let's derive the general formula one step at a time. In the first step we have added -c to both sides of the equation. Try to follow each of the other steps.
:[font = input; preserveAspect; startGroup]
WalkSolve[Q == 0, x]
:[font = print; inactive; preserveAspect]
2
c + b x + a x == 0
:[font = print; inactive; preserveAspect]
2
b x + a x == -c
:[font = print; inactive; preserveAspect]
2
b x + a x c
---------- == -(-)
a a
:[font = print; inactive; preserveAspect]
b x 2 c
--- + x == -(-)
a a
:[font = print; inactive; preserveAspect]
2 2
b b x 2 b c
---- + --- + x == ---- - -
2 a 2 a
4 a 4 a
:[font = print; inactive; preserveAspect]
2 2
(b + 2 a x) b - 4 a c
------------ == ----------
2 2
4 a 4 a
:[font = print; inactive; pageBreak; preserveAspect]
First we'll take the positive square root.
:[font = print; inactive; preserveAspect]
2
b + 2 a x Sqrt[b - 4 a c]
--------- == ----------------
2 a 2 a
:[font = print; inactive; preserveAspect]
2
b + 2 a x == Sqrt[b - 4 a c]
:[font = print; inactive; preserveAspect]
2
2 a x == -b + Sqrt[b - 4 a c]
:[font = print; inactive; preserveAspect]
2
-b + Sqrt[b - 4 a c]
x == ---------------------
2 a
:[font = print; inactive; preserveAspect]
That's the first solution.
:[font = print; inactive; preserveAspect]
Now let's take the negative square root.
:[font = print; inactive; preserveAspect]
2
b + 2 a x -Sqrt[b - 4 a c]
--------- == -----------------
2 a 2 a
:[font = print; inactive; preserveAspect]
2
b + 2 a x == -Sqrt[b - 4 a c]
:[font = print; inactive; preserveAspect]
2
2 a x == -b - Sqrt[b - 4 a c]
:[font = print; inactive; preserveAspect]
2
-b - Sqrt[b - 4 a c]
x == ---------------------
2 a
:[font = print; inactive; preserveAspect; endGroup; endGroup]
That's the second solution.
:[font = subsection; inactive; pageBreak; preserveAspect; startGroup]
The Russian quadratic formula
:[font = text; inactive; preserveAspect]
In Russia they put the general equation into a different form. The coefficient for the quadratic term is 1 instead of a and the linear term is 2 B x instead of b x.
:[font = input; preserveAspect]
R = x ^ 2 + 2 B x + C;
:[font = text; inactive; preserveAspect]
For the effort of setting up the quadratic in this more special form we get a simpler formula than QuadraticFormula. The algebra also turns out to be simpler.
;[s]
5:0,0;99,1;115,2;116,3;117,4;158,-1;
5:1,11,8,Times,0,12,0,0,0;1,10,8,Times,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,11,8,Times,0,12,0,0,0;
:[font = input; preserveAspect; startGroup]
WalkSolve[R == 0, x]
:[font = print; inactive; preserveAspect]
2
C + 2 B x + x == 0
:[font = print; inactive; preserveAspect]
2
2 B x + x == -C
:[font = print; inactive; preserveAspect]
2 2 2
B + 2 B x + x == B - C
:[font = print; inactive; preserveAspect]
2 2
(B + x) == B - C
:[font = print; inactive; preserveAspect]
First we'll take the positive square root.
:[font = print; inactive; preserveAspect]
2
B + x == Sqrt[B - C]
:[font = print; inactive; preserveAspect]
2
x == -B + Sqrt[B - C]
:[font = print; inactive; preserveAspect]
That's the first solution.
:[font = print; inactive; preserveAspect]
Now let's take the negative square root.
:[font = print; inactive; preserveAspect]
2
B + x == -Sqrt[B - C]
:[font = print; inactive; preserveAspect]
2
x == -B - Sqrt[B - C]
:[font = print; inactive; preserveAspect; endGroup; endGroup; endGroup]
That's the second solution.
:[font = section; inactive; preserveAspect; startGroup]
Implementation
:[font = subsection; inactive; preserveAspect; startGroup]
Usage message
:[font = input; initialization; preserveAspect; endGroup]
*)
WalkSolve::usage = "WalkSolve[q == 0, x] solves for the
two solutions of the quadratic equation q == 0 in the
unknown x one step at a time.";
(*
:[font = subsection; inactive; preserveAspect; startGroup]
Definitions
:[font = subsubsection; inactive; preserveAspect]
Explanation of the program
:[font = text; inactive; preserveAspect]
The variables e1, e2, e3, etc., are equations. We get the next equation by applying an algebraic operation to both sides of the previous equation. In general these equations will all be different, but it does happen in special cases that there is no change from one to the next. For example, if c = 0, e1 and e2 would be the same. WalkSolveSteps1 eliminates such repetitions in the lines redefining p up to taking the square root; WalkSolveSteps2 eliminates the rest. WalkSolve puts these two pieces together and deals with the case of a double root.
(Note that f @ g @ h @ x means f[g[h[x]]]. This notation saves a lot of nested brackets.)
:[font = input; initialization; preserveAspect]
*)
WalkSolveSteps1[q_, a_, b_, c_] := Module[
{e1, e2, e3, e4, e5, e6, p},
e1 = q == 0;
e2 = -c + First @ e1 == -c + Last @ e1;
e3 = First @ e2 / a == Last @ e2 / a;
e4 = Expand @ First @ e3 == Last @ e3;
e5 = b ^ 2 / (4 a ^ 2) + First @ e4 ==
b ^ 2 / (4 a ^ 2) + Last @ e4;
e6 = Factor /@ e5;
p = If[e1 === e2, {e1}, {e1, e2}];
p = If[e2 === e3, p, Append[p, e3]];
p = If[e3 === e4, p, Append[p, e4]];
p = If[e4 === e5, p, Append[p, e5]];
p = If[e5 === e6, p, Append[p, e6]];
Do[Print[ p[[i]] ], {i, Length @ p}];
e6
]
(*
:[font = input; initialization; preserveAspect]
*)
WalkSolveSteps2[x_, e6_, sign_] := Module[
{e7, d, e8, e, e9, f, e10, p},
e7 = PowerExpand @ Sqrt @ First @ e6 ==
sign PowerExpand @ Sqrt @ Last @ e6;
d = Denominator @ First @ e7;
e8 = d First @ e7 == d Last @ e7;
e = First @ e8 /. x -> 0;
e9 = First @ e8 - e == Last @ e8 - e;
f = First @ e9 /. x-> 1;
e10 = First @ e9 / f == Last @ e9 / f;
p = If[e7 === e8, {e7}, {e7, e8}];
p = If[e8 === e9, p, Append[p, e9]];
p = If[e9 === e10, p, Append[p, e10]];
Do[Print[ p[[i]] ], {i, Length @ p}];
]
(*
:[font = input; initialization; preserveAspect; endGroup; endGroup; endGroup]
*)
WalkSolve[q_ == 0, x_] := Module[
{a, b, c, e6},
{a, b, c} = Reverse @ CoefficientList[q, x];
e6 = WalkSolveSteps1[q, a, b, c];
If[
Last @ e6 === 0,
(* then *)
Print["We take the square root."];
WalkSolveSteps2[x, e6, 1];
Print["This is a double root."],
(* else *)
Print["First we'll take the positive square root."];
WalkSolveSteps2[x, e6, 1];
Print["That's the first solution."];
Print["Now let's take the negative square root."];
WalkSolveSteps2[x, e6, -1];
Print["That's the second solution."];
]
]
(*
^*)