Kvadratická funkce y=x^2 - 4x + 3
Quadratic function y = x2 - 4x + 3

Quadratic equation with one unknown is an algebraic equation of the second order. It can written in the form ax^{2} + bx + c, where x is the unknown and a, b, c are real valued constants. Provided a = 0, the equation is linear.

Quadratic equation can be visualized as a parabola. When a is positive, than the parabola is convex, when negative, the parabola is concave.

Solving quadratic equation

At first, we have to calculate so called discriminant:

D = b^{2} - 4ac

If the discriminant is negative, than the equation has no real solution (root), but has two distinct imaginary roots.

If the discriminant is equal to zero, than the equation has one real solution:

x ={-b \\over {2a}}

If the discriminant is positive, than the equation has two distinct real roots:


x_{1,2} = \\begin{cases} 
{{-b + \\sqrt D} \\over {2a}} 
\\\\
{{-b - \\sqrt D} \\over {2a}}
\\end{cases}

Example

Find roots of the equation x^2 - 4x + 3 = 0.

First of all, we calculate the discriminant.

D = b^{2} - 4ac = 4^2 - 4 \\cdot 1 \\cdot 3 = 16 - 12 = 4

Because the discriminant is positive, the equation has two real roots. By plugging the discriminant into the above mentioned formula, we get:


x_{1,2} = \\begin{cases}
{{{-b + \\sqrt D} \\over {2a}} = {{4 + \\sqrt 4} \\over 2} = 3} \\\\
{{{-b - \\sqrt D} \\over {2a}} = {{4 - \\sqrt 4} \\over 2} = 1}
\\end{cases}

Code

    /**
     * Solves quadratic equation in form
     * ax^2 + bx + c = 0
     * @param a
     * @param b
     * @param c
     * @return array of real valued roots, null - if the equation has no real valued solution
     */
    public static double[] solveQuadraticEquation(double a, double b, double c) {
        double d = b*b - 4*a*c; //discriminant
        if(d < 0) {
            return null;
        } else if (d == 0) {
            double[] result = {-b/2*a};
            return result;
        } else {
            double[] result = {(-b + Math.sqrt(d))/(2*a), (-b - Math.sqrt(d))/(2*a)};
            return result;
        }
    }
       /**
        * Solves quadratic equation in form
        * ax^2 + bx + c = 0
        * @param a
        * @param b
        * @param c
        * @return array of real valued roots, null - if the equation has no real valued solution
        */
        public static double[] SolveQuadraticEquation(double a, double b, double c)
        {
            double d = b * b - 4 * a * c; //discriminant
            if (d < 0)
            {
                return null;
            }
            else if (d == 0)
            {
                double[] result = { -b / 2 * a };
                return result;
            }
            else
            {
                double[] result = { (-b + Math.Sqrt(d)) / (2 * a), (-b - Math.Sqrt(d)) / (2 * a) };
                return result;
            }
        }
/**
 * Solves quadratic equation in form ax^2 + bx + c = 0
 * @param $a
 * @param $b
 * @param $c
 * @return array of real valued roots, NULL - if the equation has no real valued solution
 * @author Thomas (www.adamjak.net)
 */
function solve_quadratic_equation($a, $b, $c){
    $d = $b*$b - 4*$a*$c; //discriminant
    if($d < 0) {
        return NULL;
    } else if ($d == 0){
        $result = (-$b/2*$a);
        return $result;
    } else {
        $result = ((-$b + sqrt($d))/(2*$a) . (-$b - sqrt($d))/(2*$a));
        return $result;
    }
}







       
 

Place for your banner

Here is the position ready for our customer's banners.