Quadratic equations
18 Oct 2024Introduction
The quadratic equation is one of the fundamental concepts in algebra and forms the basis of many more complex topics in mathematics and computer science. It has the general form:
where
In this post, we’ll explore:
- What the quadratic equation represents
- How to solve it using the quadratic formula
- How to implement this solution in Haskell
What Is a Quadratic Equation?
A quadratic equation is a second-degree polynomial equation. This means the highest exponent of the variable
Quadratic equations typically describe parabolas when plotted on a graph.
The Quadratic Formula
The quadratic formula provides a method to find the values of
Here, the expression
- If the discriminant is positive, the equation has two real and distinct roots.
- If the discriminant is zero, the equation has one real (repeated) root.
- If the discriminant is negative, the equation has two complex roots.
Step-by-Step Solution
-
Calculate the Discriminant: The discriminant,
, is given by: -
Evaluate the Roots: Using the discriminant, you can find the roots by plugging the values into the quadratic formula:
If
Haskell Implementation
Now let’s translate this mathematical solution into a Haskell function. Haskell is a functional programming language with a strong emphasis on immutability and mathematical precision, making it an excellent choice for implementing mathematical algorithms.
Below, we’ll create a function quadraticSolver
that:
- Takes the coefficients
, , and as inputs. - Computes the discriminant.
- Determines the nature of the roots based on the discriminant.
- Returns the roots of the quadratic equation.
-- Haskell implementation of solving a quadratic equation
import Text.Printf (printf)
-- Function to solve the quadratic equation
quadraticSolver :: (RealFloat a, Show a) => a -> a -> a -> String
quadraticSolver a b c
| discriminant > 0 = printf "Two real roots: x1 = %.2f, x2 = %.2f" x1 x2
| discriminant == 0 = printf "One real root: x = %.2f" x1
| otherwise = printf "Two complex roots: x1 = %.2f + %.2fi, x2 = %.2f - %.2fi" realPart imaginaryPart realPart imaginaryPart
where
discriminant = b^2 - 4 * a * c
x1 = (-b + sqrt discriminant) / (2 * a)
x2 = (-b - sqrt discriminant) / (2 * a)
realPart = -b / (2 * a)
imaginaryPart = sqrt (abs discriminant) / (2 * a)
-- Example usage
main :: IO ()
main = do
putStrLn "Enter coefficients a, b, and c:"
a <- readLn
b <- readLn
c <- readLn
putStrLn $ quadraticSolver a b c
Code Breakdown:
-
Imports: We import the
Text.Printf
module to format the output to two decimal places. - quadraticSolver Function:
- This function takes three arguments:
, , and . - It computes the discriminant using the formula
. - It checks the value of the discriminant using Haskell’s guards (
|
), and based on its value, it computes the roots. - If the discriminant is negative, we compute the real and imaginary parts separately and display the complex roots in the form
.
- This function takes three arguments:
- main Function:
- The main function prompts the user to input the coefficients
, , and . - It then calls
quadraticSolver
to compute and display the roots.
- The main function prompts the user to input the coefficients
Example Run
Let’s assume we are solving the equation
Enter coefficients a, b, and c:
1
-3
2
Two real roots: x1 = 2.00, x2 = 1.00
If we try solving the equation
Enter coefficients a, b, and c:
1
2
5
Two complex roots: x1 = -1.00 + 2.00i, x2 = -1.00 - 2.00i
Conclusion
The quadratic equation is a simple but powerful mathematical tool. In this post, we derived the quadratic formula, discussed how the discriminant affects the solutions, and implemented it in Haskell. The solution handles both real and complex roots elegantly, thanks to Haskell’s functional paradigm.