Click to See Complete Forum and Search --> : quad.sh 1.1
Hello, guys I am trying to perfect my quadratic equation problem. What i want is, for it to report everything as it's being solved so I can follow along. For example, first I want to square b. Then I want 4*a*c. Then I want the squared b - the the amount of 4*a*c. Then I want to square the root of it. I would like to output everything as it is happening. Here's my problem:
#!/bin/bash
# quad.sh
# This program solves quadratic functions
# Usage: $quad.sh [a-var] [b-var] [c-var]
a=$1
b=$2
c=$3
echo "Calculating Root 1..."
R1=echo "scale=2;$b^2" | bc -ql
echo $b^2=$R1
echo -n "Root 1: "
#echo "scale=2;((-1*$b)+sqrt(($b^2)-(4*$a*$c))) / (2*$a)" | bc -ql
echo -n "Root 2: "
#echo "scale=2;((-1*$b)-sqrt(($b^2)-(4*$a*$c))) / (2*$a)" | bc -ql
The line that should be focused on is R1=echo "scale=2;$b^2" | bc -ql.
When I try R1='echo "scale=2;$b^2" | bc -ql', it echoes everything in R1 as a string. When I try it the way it is, it thinks that everything is a command whereas it also evaluates scale=2. My question is, how can you make it calcuate the square root of b, set it to a variable, and then output it?
I just thought of this idea, but it still does not work I could pipe the output of bc and assign that to R1, like so:
echo "scale=2;b^2" | bc -ql |=R1
but that does not work since the name of the var needs to be on the left and the value on the right. Even if I try to assign R1 before to 0, let's say, it still thinks that the value of R1, ie 0, is a command.
maccorin
05-17-2004, 09:14 PM
Originally posted by ecks
I just thought of this idea, but it still does not work I could pipe the output of bc and assign that to R1, like so:
echo "scale=2;b^2" | bc -ql |=R1
but that does not work since the name of the var needs to be on the left and the value on the right. Even if I try to assign R1 before to 0, let's say, it still thinks that the value of R1, ie 0, is a command.
have you tried this
R1=`echo "scale=2; b^2"|bc` ?
those are backticks not quotes
That was just what I was looking for, thanx a lot. Thanx to u, my little script is turning into a full fledged program. Here it is:
#!/bin/bash
# quad.sh
# This program solves quadratic functions
# Usage: $quad.sh [a-var] [b-var] [c-var]
help()
{
echo "
NAME
quad.sh - a quadratic solver
SYNOPSIS
quad.sh [-p] [--help] [a] [b] [c]
DESCRIPTION
quad.sh is a program that finds the two consecutive roots of quatratic
equations. In the following equation:
Ax^2-|+Bx+C
The first parameter is A, the second is B, and the third parameter is C.
OPTIONS
quad.sh may be invoked with the following command-line options:
-p Specifies the amount of significant digits to output after a
dot if a decimal
--help Prints this screen"
exit
}
if [ -z "$1" ] ; then
help
fi
if [ $1 = "-p" ] ; then
precision=$2
digit="scale=$precision"
shift 2
a=$3
b=$4
c=$5
else
precision=2 # default
digit="scale=$precision"
a=$1
b=$2
c=$3
fi
echo "Calculating..."
R1[1]=`echo "$digit;$b^2" | bc -ql`
echo "$b^2=${R1[1]}"
R1[2]=`echo "$digit;${R1[1]}-(4*$a*$c)" | bc -ql`
echo "${R1[1]}-(4*$a*$c)=${R1[2]}"
if [[ ${R1[2]} < 0 ]] ; then
echo "Cannot take the square root of a negative number!!!"
exit;
else
R1[3]=`echo "$digit;sqrt(${R1[2]})" | bc -ql`
echo "sqrt(${R1[2]})=${R1[3]}"
fi
echo
echo "Calculating Root 1..."
R1[4]=`echo "$digit;(-1*$b)+${R1[3]}" | bc -ql`
echo "(-1*$b)+${R1[3]}=${R1[4]}"
R1[5]=`echo "$digit;${R1[4]}/(2*$a)" | bc -ql`
echo "${R1[4]}/(2*$a)=${R1[5]}"
echo "Root 1: ${R1[5]}"
echo
echo "Calculating Root 2..."
R1[6]=`echo "$digit;(-1*$b)-${R1[3]}" | bc -ql`
echo "(-1*$b)-${R1[3]}=${R1[6]}"
R1[7]=`echo "$digit;${R1[6]}/(2*$a)" | bc -ql`
echo "${R1[6]}/(2*$a)=${R1[7]}"
echo "Root 2: ${R1[7]}"
#echo "scale=$precision;((-1*$b)+sqrt(($b^2)-(4*$a*$c))) / (2*$a)" | bc -ql
#echo "scale=$precision;((-1*$b)-sqrt(($b^2)-(4*$a*$c))) / (2*$a)" | bc -ql I'm think I'm going to add a -v switch if I want to output everything as it's happening or -s if I want just the two roots.
bwkaz
05-18-2004, 06:08 PM
FYI, please use [code] tags in the future, it makes programs much easier to read... ;)
Wow. Here's my final script written out with everything hopefully working. I am very proud. If I could post it somewhere where someone in the future could benefit from it, please let me know. Here it is:
#!/bin/bash
# quad.sh
# This program solves quadratic functions
# Usage: $quad.sh [a-var] [b-var] [c-var]
####################### Function Declaration Start #############
help()
{
cat<<EOF
NAME
`basename $0` - a quadratic solver
SYNOPSIS
`basename $0` [-p] [-v] [--help] [a] [b] [c]
DESCRIPTION
quad.sh is a program that finds the two consecutive roots of quatratic
equations. In the following equation:
Ax^2-|+Bx+C
The first parameter is A, the second is B, and the third parameter is C.
OPTIONS
quad.sh may be invoked with the following command-line options:
-p Specifies the amount of significant digits to output after a
dot if a decimal. Specify the number of significant digits
right after the switch
--help Prints this screen
-v Verbose Mode enabled. Prints out all the calculation to stdin
EXAMPLES
$ quad.sh -p 5 1 1 -6
Prints the roots of x^2+x-6 with 5 significant digits.
$ quad.sh -v 1 3 -3
Prints out the calculations as they are calculated
EOF
exit
}
precision()
{
digit="scale=$2"
a=$3
b=$4
c=$5
norm_op $a $b $c
}
calculate_verbal()
{
local a=$1
local b=$2
local c=$3
echo "Calculating..."
R1[1]=`echo "$digit;$b^2" | bc -ql`
echo "$b^2=${R1[1]}"
R1[2]=`echo "$digit;${R1[1]}-(4*$a*$c)" | bc -ql`
echo "${R1[1]}-(4*$a*$c)=${R1[2]}"
if [[ ${R1[2]} < 0 ]] ; then
echo "Cannot take the square root of a negative number!!!"
exit;
else
R1[3]=`echo "$digit;sqrt(${R1[2]})" | bc -ql`
echo "sqrt(${R1[2]})=${R1[3]}"
fi
echo
echo "Calculating Root 1..."
R1[4]=`echo "$digit;(-1*$b)+${R1[3]}" | bc -ql`
echo "(-1*$b)+${R1[3]}=${R1[4]}"
R1[5]=`echo "$digit;${R1[4]}/(2*$a)" | bc -ql`
echo "${R1[4]}/(2*$a)=${R1[5]}"
echo "Root 1: ${R1[5]}"
echo
echo "Calculating Root 2..."
R1[6]=`echo "$digit;(-1*$b)-${R1[3]}" | bc -ql`
echo "(-1*$b)-${R1[3]}=${R1[6]}"
R1[7]=`echo "$digit;${R1[6]}/(2*$a)" | bc -ql`
echo "${R1[6]}/(2*$a)=${R1[7]}"
echo "Root 2: ${R1[7]}"
}
norm_op()
{
echo -n "Root 1: "
echo "$digit;((-1*$b)+sqrt(($b^2)-(4*$a*$c))) / (2*$a)" | bc -ql
echo -n "Root 2: "
echo "$digit;((-1*$b)-sqrt(($b^2)-(4*$a*$c))) / (2*$a)" | bc -ql
}
####################### Function Declaration End ###################
################### Global Variable Declaration Start ##############
digit="scale=2"
a=$1
b=$2
c=$3
################### Global Variable Declaration End ################
################# Main Function of Program Starts Here ############
case $1 in
"--help")help;;
"-p")precision $1 $2 $3 $4 $5;;
"-v")calculate_verbal $2 $3 $4;;
"-*")echo "invalid switch";;
esac
if [ -z "$1" ] ; then
help
fi
if [ "3" -eq "$#" ] ; then
norm_op $1 $2 $3
fi
################# Main Function of Program Ends Here #############
maccorin
05-19-2004, 09:55 PM
Tell me who to put in the (C) line and what license you want (GPL...?)
and i'll put it on my site... i'm trying to collect useful scripts there
http://maccorin.homelinux.org
Tell me who to put in the (C) line and what license you want (GPL...?)
Thanx a lot, man I am very happy to contribute something to the community. Under copyright, you can put up "Hristo Asenov, email:njvsny@graffiti.net" and u can use the GNU General Public Licence, although you don't have to if u don't want to. I plainly don't care at all what someone does with my script, as long as they put their name on the change. You are free to change whatever bugs you find, or if you see a better way to get something done. Thanx again for posting it online.
maccorin
05-20-2004, 04:54 PM
http://maccorin.homelinux.org/scripts/quad