Search code examples
awkbc

How can I make my mathematical formula correct in bc -l or awk?


Here I want to solve a formula in bc -l or using awk.

I have some fix numbers that I can define as below:

A=5.8506
B=200.26323
C=151.3219
D=11.9275
E=0 and 5

I want to get an answer using below mathematical formula:

Ei={(B)*(C/(E*D+C))^(1/D)}^(1/3)

The answer from my formula should be 5.7965 for E=0 and 5.7965 for E=5.

Please suggest me a simple way to get answer for the mentioned mathematical formula. I did not find any code so far if it is available already.

What I have tried:

a=$(echo "$E*$D | bc -l)
echo "$a"
b=$(echo "$a+$C | bc -l)
echo "$b"
d=$(echo "$C/$b" | bc -l)
echo "$d"
E=$(echo "1/$D" | bc -l)
echo "$E"
F=$(echo "$E*$d" | bc -l)
echo "$F"

The last step should give answer for this part of my formula ( C/(E*D+C) )^(1/D), which should be 1.5232201399104 while I am getting 1.


Solution

  • Well, now it's awk:

    $ awk -v E=5 '
    BEGIN{
        A=5.8506
        B=200.26323
        C=151.3219
        D=11.9275
        Ei=((B)*(C/(E*D+C))^(1/D))^(1/3)
        print Ei
    }'
    5.79653
    

    or

    $ awk -v A=5.8506 -v B=200.26323 -v C=151.3219 -v D=11.9275 -v E=0 '
    BEGIN{                        
        Ei=((B)*(C/(E*D+C))^(1/D))^(1/3)
        print Ei
    }'
    5.8506