Friday, January 25, 2013

Completing The Square

Factoring is an easy and satisfying way to solve a quadratic equation. Problem is not all quadratics can be factored. In fact only a small number of them can. To be exact, only quadratic equations with rational roots can be factored by traditional means.

Completing the square allows you to get around this problem. The basic idea is to take any quadratic equation and force it into a perfect square factor-able state. Math is very violent sometimes...

So now that you know the why and the idea behind the how, lets see how its done.

First thing before solving any quadratic is to make sure it's in the correct form for using a given technique. Factoring and using the quadratic equation want the equation in standard form:

$$ax^2 + bx + c = 0$$

With completing the square you end up isolating $x$ on the left hand side and putting your equation on the right. So you start out moving $c$ to the right side of the equals sign like so:

$$ax^2 + bx = -c$$

The next step is you divide each term by $a$ to get the equation into a monic form. This means that the coefficient of the the $x^2$ term is one. $$ x^2 + \frac{b}{a}x = \frac{-c}{a} $$

Next thing you want to do is take the value of \(\frac{b}{a}\) divide it by $2$ then square it all. Then add that to both sides of the equation. This is the real magic in completing the square. In any perfect square, the constant term we are adding to both sides has to be $(\frac{b}{2})^2$. This is what makes the perfect square work. $$ x^2 + \frac{b}{a}x + \frac{b^2}{4a^2} = \frac{-c}{a} + \frac{b^2}{4a^2} $$

Here is an image to help you understand what we just did to the left side to complete the square



Let's get the right side of the equals sign simplified by using a common denominator to add the two fractions. We do this by multiplying \(\frac{-c}{a}\) by \(\frac{4a}{4a}\) $$ \bigg( \frac{-c}{a} \times \frac{4a}{4a} \bigg) + \frac{b^2}{4a^2} = \frac{-4ac + b^2}{4a^2} $$

So now we have: $$ x^2 + \frac{b}{a}x + \frac{b^2}{4a^2} = \frac{-4ac + b^2}{4a^2} $$

Now to simplify the the left side the same way: $$ \bigg( \frac{x^2}{1} \times \frac{4a^2}{4a^2} \bigg) + \bigg( \frac{b}{a}x \times \frac{4a}{4a} \bigg) + \frac{b^2}{4a^2} = \frac{4a^2x^2 + 4abx + b^2}{ 4a^2} $$

Leaving us with: $$ \frac{4a^2x^2 + 4abx + b^2}{ 4a^2} = \frac{-4ac + b^2}{4a^2} $$

Now we can multiply both sides by $4a^2$ to get rid of the denominator. $$ {4a^2} \times \frac{4a^2x^2 + 4abx + b^2}{ 4a^2} = \frac{-4ac + b^2}{4a^2} \times 4a^2 $$ Now we have: $$ 4a^2x^2 + 4abx + b^2 = -4ac + b^2 $$ Ok, we are getting close. Now we can factor the left side as we have completed the square, so the left side is now a perfect square. $$ \begin{align*} (2ax+b)(2ax+b) &= -4ac + b^2 \\ (2ax+b)^2 &= -4ac + b^2 \end{align*} $$

Now lets take the square root of both sides: $$ \begin{align*} \sqrt{(2ax+b)^2} &= \pm \sqrt{-4ac + b^2}\\ 2ax+b &= \pm \sqrt{-4ac + b^2} \end{align*} $$

Now to finish isolating $x$ we will subtract $b$ from both sides and divide by $2a$ $$ \begin{align*} 2ax &= -b \pm \sqrt{-4ac + b^2}\\ \\ x &= \frac{-b \pm \sqrt{-4ac + b^2}}{2a} \end{align*} $$

We're done! What's that you say? YES, it is the quadratic formula! The quadratic formula is derived by completing the square! Sometimes it's easier to just use the quadratic formula outright, other times you might prefer to complete the square.

Let's try an example of completing the square. We will use the same equation we used in the last post: $x^2 - 4x - 12 = 0$. You will notice that this one can be factored 'normally', but we will use completing the square anyway. This example is already a monic equation so no need to divide all terms by $a$. $$ \begin{align*} x^2 - 4x - 12 &= 0\\ \\ x^2 - 4x &= 12 \\ \\ x^2 - 4x + 4 &= 16 \\ \\ (x - 2)(x - 2) &= 16\\ \\ (x - 2)^2 &= 16\\ \\ \sqrt{(x-2)^2} &= \pm \sqrt{16}\\ \\ x-2 &= \pm 4\\ \\ x &= 2 \pm 4\\ \\ x &= 6 \; or \; x = -2 \end{align*} $$

There you have it. You now know, if you didn't already, how to complete the square. You also saw how the quadratic equation is derived. I hope this has been informative. Please leave a comment if you have any questions or suggestions. Thanks.

Factoring Quadratic Equations

When I was in 10th grade I took Algebra II. I was more focused on women and other stuff, so a lot of what was taught just didn't stick. What did stick didn't seem to have any context. So it didn't really mean anything to me. Just some more stuff to memorize for the up coming test. This really took the fun out of math for me. Up until that class I understood math well and really enjoyed it.

Anyway, here I am 20 years later :s Now I want to understand math. Not just puke it up on demand, not really understanding what's happening. With that in mind, lets talk about quadratic equations!! \o/

A quadratic equation is a polynomial of the second degree with one variable. Second degree means the highest power any term is raised to is 2.

Example:

\(x^2 - 4x = 12\)

I know of 3 ways to solve quadratic equations.

  • Factoring
  • Completing The Square
  • Quadratic Formula

Of the 3 I think I enjoy factoring the most, so we will discuss it first. Factoring, as you probably know, is just busting up a number or expression into smaller parts that when multiplied together give you the expression you started with. This is useful for reducing a number or expression into simpler parts or solving quadratic equations :) If you need more information check this out

The first thing we do is rewrite the equation in standard quadratic equation form. a, b, and c below just reprensent some constants like 1, -4, and -12.

\(ax^2 + bx + c = 0\)

This means setting the whole thing equal to zero. This is useful when factoring because you know that one of the factors has to be zero. The only way you can get zero as the product of multiplication is if one of the number you are multiplying by is zero. By doing this you can more easily solve for the variable.

Example:

\((x + 3)(x - 2) = 6\)

vs

\((x - 3)(x + 4) = 0\)

In the second example you know that one of the factors must evaluate to zero. So in that example x could be $3$ or $-4$. In the example above it x is much harder to solve for because we must examine both factors to see how the product can produce $6$. I hope that makes sense :s

Once you have the equation in quadratic form you can then try to factor it. Not all equations can be factored. When you can't you must use some other method like the quadratic equation to solve for x. With all that said lets try one.

\(x^2 - 4x = 12\)

First lets put it in the standard form of \(ax^2 + bx + c = 0\).

\(x^2 - 4x - 12 = 0\)

With that done now we can factor it:

\((x - 6)(x + 2) = 0\)

Now we know that $6$ or $-2$ are solutions for x. That's it, equation solved! Let's just check our answers to be sure.

First we will plug in $6$ and see if it works.

$$ \begin{align*} 6^2 - 4(6) - 12 = 0\\ 36 - 24 - 12 = 0 \end{align*} $$

Looks good to me. Lets try with $-2$.

$$ \begin{align*} (-2)^2 - 4(-2) - 12 = 0\\ 4 + 8 - 12 = 0 \end{align*} $$

So there you have it. One thing to note, if you are dealing with an equation of degree 2, then you will have 2 roots (answers) for that equation. See this for more information.

This weekend I will cover completing the square and the quadratic formula. Then, for completeness, I will show you how to derive the quadratic formula and a reverse proof for it. Oh the fun we will have! Thanks for reading. Please comment if I missed something or you have any questions.

Wednesday, December 19, 2012

Associative, Commutative, and Distributive Properties

Associative, Commutative, and Distributive Properties are some simple rules for working with mathematical equations and expressions.

The associative property of addition and multiplication basically states when you are performing the same operation multiple times in a row, the order in which they are performed doesn't matter. You can group with parentheses anyway you want and it will not change the final value.

Examples:

Addition:
a+b+c = (a+b)+c = a+(b+c)
2+3+4 = (2+3)+4 = 2+(3+4) = 9

Multiplication:
abc = (ab)c = a(bc)
2*3*4 = (2*3)*4 = 2*(3*4) = 24

So as you can see the order that we performed the computations did not matter. To be clear you can not mix the operators up and still expect this to work. For example consider the following:

2*3+4 != 2(3+4)

In this case since the operators are not the same you can't place the parentheses anywhere you want. PEMDAS NEVAR 4 GET!

The commutative property of addition and multiplication says you can change the order around however you like and the final result will be the same. This does not mean you can perform the operations in any order you want, just that you can move stuff around...

Examples:

Addition:
a+b+c = b+a+c = c+b+a
2+3+4 = 3+2+4 = 4+3+2

Multiplication:
abc = bac = cab
2*3*4 = 3*2*4 = 4*2*3

Mixed: a+b+c*d = (d*c)+b+a

You can also you subtraction if you think of it like adding a negative

Example:
a+b-c = -c+a+b
1+2-3 = -3+2+1 = 0

The distributive property says that multiplication of real numbers distributes over addition or subtraction of real numbers.

Examples:
4(3+4) = (4*3) + (4*4) = 28
4(3-2) = (4*3) - (4*2) = 4
4(x+y) = 4x + 4y
4(x-y) = 4x - 4y

Friday, November 30, 2012

Greatest Common Factor

Greatest Common Factor (GCF) is something you learn in grade school. It is still fascinating to me.

Greatest Common Factor, also known as Greatest Common Divisor is:

(from Wikipedia)

GCF of two or more non-zero integers, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.

You might have used GCF to reduce a fraction to its simplest form, also known as coprime (prime relative to each other).

GCF can be calculated a few ways. One fun way is to reduce both numbers to their prime factors. Then tally up what they have in common. Here are the prime factor trees for 1000 and 350.

The common prime factors between these 2 numbers are: 2 * 5² . So 50 is the GCF. Doing a prime factor tree for bigger numbers can take a while and be messy.

In Euclid's Elements exists an algorithm to compute the GCF much easier. In his time the concept of Real Numbers did not exist. So GCF was expressed geometrically. The GCD of two lengths a and b corresponds to the greatest length g that measures a and b evenly; in other words, the lengths a and b are both integer multiples of the length g.

Now days this idea is easier expressed with numbers. Here is the formula:


This can also be expressed with floored division instead of mod.


So, if you wanted to use this formula with pen and paper its easy. You divide 1000 by 350 (to use our example above) Then divide 350 by the remainder, then divide the remainder of that into the previous remainder an so on until you reach a division operation that produces no remainder. The divisor, or the number you used to divide with, that resulted in no remainder, is the GCF! YAY.


Lets look at a few implementations in Groovy. lameGcd iterates from the value of the smallest number down to 2 looking for numbers that will divide into both with no remainder. This means that in a worst case you would have to iterate from the value of num to 2.
def lameGcd = 
{ num, denom ->
 for(int i = num; i > 1; i--)
 {
  if(num%i==0 && denom%i==0) return i
 }
 return 1
}
We can do better using the Euclidean algorithm:
def edGcd = { num, denom -> !(denom%num) ? num : edGcd(denom%num, num) }
Here is a version that doesn't care which number is bigger:
def euclidDivisionGCD = { a, b ->
 def big = Math.max(a,b) 
 def small = Math.min(a,b)
 return !(big%small) ? small : call(small, big%small)
}
It's getting late. Next I'll carry on about Least Common Multiple :)

Wednesday, November 28, 2012

Eclipse Juno

I recently downloaded the current version of the Eclipse IDE. It's called Juno, or 4.2 if you don't have the time to keep up with names (like myself).



I was very excited to use the new version. In the past every time I updated it seemed a host of bugs were fixed and life was good. The Groovy and Grails support generally improves dramatically between releases. 4.2 did not let me down in that area. The editors and over all support for Groovy and Grails are getting pretty good! :)

However, the inspiration for this post is not the good, because the good alone doesn't warrant a post. If the new features were amazing or something, sure a post is in order. That is not the case. Eclipse has updated the UI.... I'm all for pretty new UIs, but only if they work and don't take away from usability.

Previous versions of Eclipse handled screen splitting and tab dragging perfectly. I would change nothing. I have 3 displays, and I am constantly dragging tabs between them. I also drag tabs off to split up the editor on one display so I can see multiple files at once in the same editor.

Well, all that is now busted. I can't drag between editors anymore. Dragging at all seems to make Eclipse stutter and sometimes lock up for a few seconds, but it feels like eons. Now when you drag a tab off an editor, instead of being placed into another existing editor, one that's directly below your mouse, a new editing...window is created. If you hit ctrl+shift+r in that window, the file open dialog opens up, but when you select a file to open, it's opened in the original editor, not in the new window you created.....ARGHHHGHHH.

Lastly, Eclipse is a lot more sluggish now.. I mean it is a Java desktop application. So I allow for a certain amount of hesitation. With 4.2 though, I feel I am often waiting for the screen to repaint :(

So yeah, Eclipse for multiple monitors is pretty well busted. I hope it's fixed soon. Eclipse is the reason I have 3 displays...

Permutations and Combinations With Groovy

fac: work out the factorial of any positive integer
comobs: given n amount of things, and picking r amount from it, where order does not matter, how many combos are there?
permutations: give n amount of things, picking r amount from it, where order matters, how many permutations are there?

def fac = {it = it as BigInteger;  it <= 1 ? 1 : it * call(it - 1)}

// c(n,r) = n!/r!(n-r)!
// n = 52 cards  ; r = 5 cards per hand
// order doesnt matter!!
def combos = { n, r -> fac(n).divide(fac(r)*fac(n-r))}

// P(n,r) = n!/(n-r)!
// order matters!!
def permutations = { n, r-> fac(n).divide(fac(n-r))}

combos(52,5)
2598960
permutations(10, 2)
90