Equations - Solving Problems with BASIC
User Rating: / 0
Written by Bryce Ringwood   

Equations vs Formulae

I 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 R1 and R2 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{  { {x-3}\over{x-56} } = 2.763   }

These linear equations often yield useful formulae, which can be repetiively solved using a BASIC program, or your calculators' own language.

Introducing BASIC

Life'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.  GW-BASIC 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 new, then the program, as follows:

20 PRINT "Enter Values for R1 and R2"
30 INPUT R1,R2
40 PRINT "R = " , R1*R2/(R1+R2)
60 END
Enter Values for R1 and R2 ? 10,30
R =             7.5
Break in 50

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 equations

Quadratic 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)}}


L= inductance in µH
r = coil radius in inches
l = coil length in inches
n = number of turns


ar^2 + br + c = 0


\displaystyle {a = n^2}
c = -10 l L

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

10 REM quadratic equation solver
20 PRINT "Enter a,b,c",
40 D = B^2 - 4*A*C
50 IF D < 0 GOTO 100
60 PRINT "Root 1 = ",(-B+SQR(D))/(2*A)
70 PRINT "Root 2 = ",(-B-SQR(D))/(2*A)
100 PRINT "Roots are complex -not calculated"
110 STOP
120 END
Enter a,b,c   ? 2,4,1
Root 1 =      -.2928932
Root 2 =      -1.707107
Break in 80 

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 ?

Enter a,b,c   ? 2,4,.00001
Root 1 =      -2.503395E-06
Root 2 =      -1.999998
Break in 80 

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 cubic

Here 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)}

\displaystyle{x^3 + x^2(p-1)+x(q - p) -q}

Clearly, q=1 and p also=1 because there were no x or x-squared 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}}}.

Sharp PC E500
      SHARP PC-E500S of the late '90s . Takes cube roots of negative numbers. It is programmable in BASIC.  

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 PC-E500S "largepocket"calculator. It has a cube-root 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 equations

These 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}

\displaystyle {3\times -2 + 4y=12;   y = 18/4 = 4.5}

This method of elimination and back-substitution (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"laugh has a simultaneous equation solver built-in.

A common simultaneous equation algorithm is the Gauss-Jordan 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:

Simultaneous Equations using SMATH

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. 

  • Equations may be used to develop formulae. Formulae are equations, but not all equations are formulae
  • Quadratic equations have two roots, cubics have three and so on
  • Some calculators and computers* can be programmed to automatically perform repetitive calculations
  • The way numbers are represented in computers and calculators can pose precision problems - always look to see if the answer makes sense
  • Matrices and Vectors are used as a form of shorthand to represent systems of equations.

* "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 Problem

The text below was simply cut and pasted into this article. CalcPad (at the moment) can't process matrices and vectors.  Its really a word-processor that does calculations.


Solving square rootsCalcPad doingsquae roots




Set precision using fix


Solution 1


x1 = (-b + Sqr(b^2-4*a*c))/(2*a)

Result : -0.29289


Solution 2

x1 = (-b - Sqr(b^2-4*a*c))/(2*a)

Result : -1.70711







Appendix - Decimal BASIC

This 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!).




Joomla template by a4joomla