Equations  Solving Problems with BASIC 
Written by Bryce Ringwood  
Equations vs FormulaeI think of a formula as an equation that has been solved. Maybe that's a bit simplistic, I don't know. At any rate, we all think of the formula for degrees Celsius in terms of Degrees Fahrenheit, or the formula for Resistors in parallel or .. whatever. There it is the thing we want to calculate on the left side of the equals sign, and all the stuff we need to calculate it in placeholders on the right. Take the equation for resistors in parallel, the combined resistance is R made up from R_{1 }and R_{2 }in parallel: \displaystyle{{1\over{R}}={1\over{R_1}}+{1\over{R_2}} }
This is an equation in R, whose solution is the formula:
\displaystyle{{R} = {{ R_1R_2}\over{R_1+R_2}}}
(To solve the equation,multiply each side by R_1R_2R and divide by R_1+R_2.) So, equations are the puzzle we have to solve in order to arrive at a formula. The simplest type of equation to solve is the on like the resistance formula above, where we only need separate out one variable, and it is unencumbered with square roots, squares, logs or anything else. At this point,most textbooks will ask you to solve about a gazillion examples, prefaced with Solve for X: \displaystyle{ { {x3}\over{x56} } = 2.763 }
These linear equations often yield useful formulae, which can be repetiively solved using a BASIC program, or your calculators' own language. Introducing BASICLife's too short to remember keystrokes and do dumb repetitive calculations over and over again. It's also too short to mess about with complicated programming languages and linkers and loaders etc. BASIC is easily learned and natural to use  provided we stick to the original Dartmouth Basic with line numbers and don't get all het up about the goto statement. GWBASIC is "abandonware" (I think) and is close to Dartmouth BASIC. It runs on a PC  so that's what we'll use. You need to copy GWBasic.exe to a directory and run it by typing gwbasic at the command prompt. Note! From Windows 7 onward, GW Basic might cause problems with your computer. I will try to post some alternatives in appendix. To get out of gwbasic, type system.
To begin our resistor program, type
10 REM RESISTOR PROGRAM If you are using a calculator, there may be some slight differences. Please don't use the Raspberry Pi's "tinybasic" as it is extremely limited mathematically. That's It!  You can now program in BASIC.
Quadratic equationsQuadratic equations are used for ... not very much, as far as I can see. Unless you're into gunnery or something. Maybe you need to work out the radius of a coil, knowing the turns, the inductance and the length. Its a very contrived application, I'm sure you will agree.:
L =\displaystyle{ {r^2 n^2} \over{ (9 r + 10 l)}}
where
L= inductance in µH Rearranging: ar^2 + br + c = 0
where:
\displaystyle {a = n^2} Now, we have to find a means of solving the equation. Perhaps the simplest way is to cast it into the format: \displaystyle{ (r +something)^2 + constant = 0}
First we divide throughout by a: \displaystyle{r^2 + {rb\over a} + {c \over a} = 0}
Next we complete the square: \displaystyle{\left(r + {b\over 2a} \right)^2 {\left(b\over 2a\right)}^2 + {c\over a} = 0}
Then we can solve the equation by taking square roots: \displaystyle{ { r + {b\over 2a}} = \pm \sqrt{ {\left(b\over 2a\right)}^2  {c\over a} } }
Giving us the well known equation:
\displaystyle {{r }={ {b \pm \sqrt {b^2  4ac}}\over {2a}}}
(except most textbooks use x not r!) What happens if \displaystyle{b^2  4ac \lt 0} ?  Well then we have to use the imaginary unit i.
\displaystyle {{r }={ {b \pm i \sqrt {b^2  4ac}}\over {2a}}}
This is a complex number  it contains a real part and an imaginary part. We will look at these in a later article.The important thing to note is that the equation has two solutions  either two real or two complex. Automating the solution with BASIC
list Automating the solution with SMATH (Only one root shown) . The only problem with SMATH is that you can't cut and paste or export except as images. (Which is why I haven't removed CalcPad.)
When you do a calculation on a computer (in particular) or a calculator  be careful of problems with precision. Does this make sense ?
Well  no actually. Root 1 has in reality lost all its precision or accuracy. GWBASIC only works to 6or 7 digits of precision maybe more if you use double precision (not covered here). All you an really say is that Root1 is approximately\displaystyle{ 2.5*10^{6}}
The Cube Roots of Unity  solving the cubicHere we look in a little more detail at that vexing calculator problem  why won't my calculator do the cube root of 8? I must warn you, this section uses complex numbers, which I will cover later on. Just remember \displaystyle i^2 =  1 and you should follow along. Lets look at the equation: \displaystyle{x^3 1 = 0}
It is a cubic  so it must have three roots. Obviously x = 1 is one of them  what are theother two ? First we factorise
\displaystyle{x^3 1 \equiv (x  1)(x^2 + px + q)} yielding:
\displaystyle{x^3 + x^2(p1)+x(q  p) q}
Clearly, q=1 and p also=1 because there were no x or xsquared in the orginal equation so: \displaystyle{x^3 1 \equiv (x  1)(x^2 + x +1)}
The second set of brackets is a quadratic with roots \displaystyle{{{{1\over2}}  {{i \sqrt 3}\over 2}} and {{{1\over2}} +{ {i \sqrt 3}\over 2}}}.
These are sometimes referred to as \displaystyle {\omega and {\omega}^2}. You might like to verify these are correct. Of course, a simple calculator can't provide you with three answers for the cube root of 1 (or 8), so it will just give you the value for the real root. If you go through the logic for x cubed =  1, you will get roots of 1, \displaystyle {\omega and {\omega}^2}  so your calculator thows up its metaphorical hands and gives you an error message. ( WELL  Not the SHARP PCE500S "largepocket"calculator. It has a cuberoot function which happily works with negative numbers.  I just discovered) You can read more about solving cubic and higher order polynomial quations on the Internet and elsewhere. Look up "Cardano's solution of the cubic". Simultaneous equationsThese are sets of linear equations in the same number of unkowns quantities. Let's call them x and y. Here's an example of two equations in two unknowns x and y: \displaystyle {3x + 4y = 12}
\displaystyle {9.5x + 8y = 17}
Multiply the first equation by two and subtract the first from the second:
\displaystyle {3.5x = 7; x = 2} and
\displaystyle {3\times 2 + 4y=12; y = 18/4 = 4.5}
This method of elimination and backsubstitution (as it is called) can be caried out manually for two or three sets of equations. Any more than that and I guarantee you will get the wrong answer. There are, however, a number of systems that will allow you to solve these equations without going mad. The "SHARP of course" has a simultaneous equation solver builtin. A common simultaneous equation algorithm is the GaussJordan elimination method  but only a machine should use it. For once, I'm going to deter you from writing your own simultaneous equation solver because there are quite a few pitfalls to be avoided, particularly if you need to use it professionally. By far the easiest way to solve simultaneous equations, is to use SMATH. You have to represent the equations in matrix form. A matrix is simply an array of numbers, which we treat as a single unit. A vector is just a one dimensional matrix  another row or column of numbers. These are represented in brackets. I think the example is much easier to follow than anything I could describe in words:
This explains matrix maths in a simple way. It is very easy to enter all the variables. Its dead easy to correct a faux pas, Its fun and has everything going for it except one thing  how reliable is the matrix inversion routine ? Will it deliver spurious results ? Someone else must provide the answers to those questions. Summary
* "Computer" in this sense being something having a "programming language" available. The way things are going, we will all be programming Javascript on our mobile phones. Maybe the Raspberry Pi will save the world. Maybe it really will ... Appendix  Using CalcPad to Solve the Square Root ProblemThe text below was simply cut and pasted into this article. CalcPad (at the moment) can't process matrices and vectors. Its really a wordprocessor that does calculations.
Solving square roots a=2 b=4 c=1 Set precision using fix Fix=5 Solution 1 ? x1 = (b + Sqr(b^24*a*c))/(2*a) Result : 0.29289 ? Solution 2 x1 = (b  Sqr(b^24*a*c))/(2*a) Result : 1.70711 Appendix  Decimal BASICThis is a windows program that fulfils the requirements:
Decimal BASIC running the example program Microsoft provide a free BASIC called Small Basic. It is easy to use, certainly, but it is a long way away from the original BASIC that calculators such as the HP85/87, SHARP, and the IBM PC used to use. Actually, it reminds me of VB6 a bit. What is commendable is that Microsoft are giving Joe Public a development platform free. You can also use "Return to BASIC" for the Raspberry Pi. There's also something called "Phone BASIC" for Windows 8 phones. (It works but ...  Good Luck with that!).
