Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #Dichotomy method
- #Equalation acoef*x^2+bcoef*x
- acoef="0.5"
- bcoef="2"
- #[a;b]
- a="-2"
- b="-3"
- #Epsilon, accuracy
- e="10^-6"
- #BC returnes 1 if true and 0 if false
- #BC has no ABS function by default
- statement=$(echo "define abs(x) {if (x<0) {return -x}; return x;}; abs($b - $a) > $e" | bc)
- # [[ works only in bash shell, newer ]]
- # [ works in default shell ]
- while [[ $statement = 1 ]]
- do
- #scale makes 6 digits after comma
- c=$(echo "scale=6; ($a + $b) / 2" | bc)
- statement=$(echo "scale=6; ($b * $b * $acoef + $b * $bcoef) * ($c * $c * $acoef + $c * $bcoef) < 0" | bc)
- if [[ $statement = 1 ]]
- then
- a=$c
- else
- b=$c
- fi
- statement=$(echo "define abs(x) {if (x<0) {return -x}; return x;}; abs($b - $a) > $e" | bc)
- done
- echo "scale=6; ($a + $b) / 2" | bc
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment