Solving Quadratic Equation

Script for solving quadratic equation

In mathematics , a quadratic equation is a univariate polynomial equation of the second degree. A general quadratic equation can be written in the form
ax^2+bx+c=0,\,
where x represents a variable or an unknown, and a, b, and c are constants with a ≠ 0. (If a = 0, the equation is a linear equation.)
                                          -wikipedia ( http://en.wikipedia.org/wiki/Quadratic_equation )


-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
If  b² - 4ac ≥ 0, then the roots of the quadratic equation ax²+bx+c=0 are given by (-b ± √(b² - 4ac)) 2a This formula for finding the roots of the quadratic equation is known as the quadratic formulae.

-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
#!/bin/bash

#for finding the value of x in "ax^2 + bx + c = 0"

echo -e "\e[1;34ma\e[1;31mx^2 \e[0m+ \e[1;34mb\e[1;31mx\e[0m + \e[1;34mc\e[0m = 0"
read -p "a=" a
read -p "b=" b
read -p "c=" c

#finding value of x using quadratic formula
x1=$(echo "scale=2; ((-1*$b) + (sqrt(($b*$b)-(4*$a*$c))))/(2*$a)" | bc 2> /dev/null)
x2=$(echo "scale=2; ((-1*$b) - (sqrt(($b*$b)-(4*$a*$c))))/(2*$a)" | bc 2> /dev/null)

#in case the value of x cannot be found
if [[ $x1 == $null && $x2 == $null ]];then
    echo -e "\e[1;31mvalue of x cannot be found!!\e[0m"
else
    echo -e "\e[1;31mx\e[0m=$x1, $x2"
fi


-------------------------------------------------------------------------------------------



In my system the script is named quadratic.To solve   x^2+4x+4=0 , execute quadratic in terminal ..





HAA !! solved..

Enjoy Linux !!


Don't know what to do with these codes ?? click here

1 comment: