How do you turn lights off?
Well wonder no further!
The damage output of all of your units can be expressed as a simple formula
number_of_units * (base_damage_of_unit + damage_increase_per_upgrade * number_of_upgrades)
The total cost of all of your units can be expressed as the sum of the costs of all the units you build, and the sum of the costs of all of your upgrades.
total cost = cost_of_all_units + cost_of_all_upgrades
The cost of all of your units is just the cost of one unit times the number of units you build
cost_of_all_units = cost_of_one_unit * number_of_units
The cost of the upgrades can vary. Assuming that the costs of the upgrades increases arithmetically, (1,2,3,4,5 minerals for the 1st, 2nd, 3rd, 4th, and 5th upgrades) then the total cost of all your upgrades can be expressed as the (sum of the first term plus the sum of the second term) times half the number of terms.
aside**
+ Show Spoiler +
Gauss was the original G-unit. Look at him.
I bet he wasn't afraid to divide by zero.
Professors often talk about how kid Gauss figured out this mathematical phenomena.
See teachers in the 1800's were really really lazy. They were so lazy in fact, that Gauss's elementary school teacher, told his students to add up the first 100 whole numbers, while he went to take a nap. As the others students scurrilously started adding numbers on their papers, Gauss figured out that, adding 1 and 100, produced the same result as adding 2 and 99. He just added the sum of 1 and 100 and multiplied by the number of pairs of number he would have.
He figured out the sum of the first 100 whole numbers (5050), woke his teacher up from his nap, and went on to develop lots of ways to torment engineering students taking electromagnetics courses.
Though best known as the inventor of the gauss rifle, gauss also did a lot of math in his spare time.
cost_of_all_upgrades = number_of_upgrades / 2 * (sum_of_first_and_last_upgrade)
and sum_of_first_and_last_upgrade can be defined as number_of_upgrades + 1 (the first upgrade costs one mineral, and the last one costs the same amount as the number of upgrades you've gotten).
Knowing all this, you can optimize damage with respect to cost by putting all the equations together.
{damage}/{cost} = { number_of_units * (base_damage_of_unit + damage_increase_per_upgrade * number_of_upgrades) } / { [ cost_of_one_unit * number_of_units ] + [ number_of_upgrades / 2 * (number_of_upgrades + 1) ] }
Now you break out your handy excel spreadsheet!... What you don't have a handy excel spreadsheet? Fine... you can borrow mine.
It tells you when you should upgrade and when you should build. If only you'll fill in some numbers.
I understand that damage calculations have lots of confounding variables, such as splash damage, range, or the speed of projectiles. I made this blog mainly to get a 1000th post. Theorycrafting can be useful for things like saturation calculations but that's a bit more complicated.
I personally don't play defence maps. There are too many newbies who make you lose. The only defence maps I play are SAT control, and ones where I can play individually.