One of the main purposes of programs is to simplify often-performed tasks. In mathematics, these tasks often take the form of common and important equations---for example, the quadratic formula, used to find the roots or "zeroes" of a quadratic equation---the points where it crosses the x-axis. This article will show how to construct a TI-BASIC program that will do the quadratic formula, while illustrating a few of the program's key points. The formula is higher-level algebra, but this article won't go too in-depth mathematically.
The quadratic formula is as follows:
If a, b, and c are all constants, and
The first thing we need to do is to ask the user for a, b, and c.
Something like this:
:Input A :Input B :Input C
would work, but doesn't it seem a bit unnecessary to do the same thing three times? We can simplify the programming for ourselves.
will do the same thing in just one line of code. This illustrates an important rule of thumb for programming: in general, a program should not contain duplicate or very similar pieces of code. In this case, we're lucky because TI-BASIC provides a way to combine multiple Input commands, but you should always be looking for ways to combine parts of your programs.
Performing the CalculationEdit
Now all we need to do is calculate and display the results. Here's the quadratic formula in calculator terms:
:Disp (-B+√(B^2-4AC))/(2A) :Disp (-B-√(B^2-4AC))/(2A)
That will store and print the results in the variables X and Y (the "plus-or-minus" sign in the formula means that there are two results). Similar to the way we input numbers in the first step, TI-BASIC allows us to display to values without writing the same line twice. This code may be re-written as:
Now the program should be ready. Go ahead and try it out with A = 1, B = 1, and C = -6. You should get solutions of 2 and -3.
Let's try it out with A = 2, B = 1, and C = 3. You should get output like the following (if your calculator is in REAL mode and not a+bi mode):
ERROR: NONREAL ANSWERS
Whoops! What does that mean?
Take a look at the "discriminant" of the quadratic formula (the part under the radical, that we take the square root of):
In our situation, that would be:
The problem with this is that the square root of a negative number is nonreal, or imaginary. In standard mode, calculator tries to avoid nonreal answers, so anything containing an imaginary number is considered an error.
Now, you might just say, "Well, I'm not going to be dealing with imaginary numbers," but the problem is that you want your program to be as useful as possible. Maybe you'll be using imaginary numbers at some point, and then you'll want your program to take them into account.
This is the simple fix to the problem:
- Go back to editing your program.
- Make a new line at the very beginning.
- Press MODE
- Press ENTER on a+bi.
The whole program should now look like this:
And you have a complete program!
Another possibility for the program takes advantage of list behavior:
This will return a two-entry list containing the two answers.